Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
PHP | date_parse_from_format() Function
Next article icon

NLP | Using dateutil to parse dates.

Last Updated : 18 Jun, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
The parser module can parse datetime strings in many more formats. There can be no better library than dateutil to parse dates and times in Python. To lookup the timezones, the tz module provides everything. When these modules are combined, they make it very easy to parse strings into timezone-aware datetime objects. Installation : dateutil can be installed using pip or easy_install, that is, sudo pip install dateutil==2.0 or sudo easy_install dateutil==2.0. 2.0 version for Python 3 compatibility is required. The complete documentation can be found at http://labix.org/python-dateutil. Code: Parsing Examples Python3 1==
# importing library from dateutil import parser  print (parser.parse('Thu Sep 25 10:36:28 2010'))  print (parser.parse('Thursday, 25. September 2010 10:36AM'))  print (parser.parse('9 / 25 / 2010 10:36:28'))  print (parser.parse('9 / 25 / 2010'))  print (parser.parse('2010-09-25T10:36:28Z')) 
Output :
  datetime.datetime(2010, 9, 25, 10, 36, 28)  datetime.datetime(2010, 9, 25, 10, 36)  datetime.datetime(2010, 9, 25, 10, 36, 28)  datetime.datetime(2010, 9, 25, 0, 0)  datetime.datetime(2010, 9, 25, 10, 36, 28, tzinfo=tzutc())
All it takes is importing the parser module and calling the parse() function with a datetime string. The parser can return a sensible datetime object, but it cannot parse the string, it will raise a ValueError. How it works :
  • The parser instead of looking for recognizable tokens, guess what those tokens refer to. It doesn't use regular expressions.
  • The order of these tokens matters as it uses a date format that looks like Month/Day/Year (the default order), while others use a Day/Month/Year format.
  • The parse() function takes an optional keyword argument, dayfirst, which defaults to False to deal with this problem.
  • It can correctly parse dates in the latter format if it is set to True.
Python3 1==
parser.parse('16 / 6/2019', dayfirst = True) 
Output :
datetime.datetime(2016, 6, 16, 0, 0)
Another ordering issue can occur with two-digit years. but '11-6-19' is an ambiguous date format. Since dateutil defaults to the Month-Day-Year format, '11-6-19' is parsed to the year 2019. But if yearfirst = True is passed into parse(), it can be parsed to the year 2011. Python3 1==
print (parser.parse('11-6-19')) print (parser.parse('10-6-25', yearfirst = True)) 
Output :
datetime.datetime(2019, 11, 6, 0, 0)  datetime.datetime(2011, 6, 19, 0, 0)
dateutil parser can also do fuzzy parsing and allows to ignore extraneous characters in a datetime string. parse() will raise a ValueError with the default value of False, when it encounters unknown tokens. A datetime object can usually be returned, if fuzzy = True.

Next Article
PHP | date_parse_from_format() Function

M

mathemagic
Improve
Article Tags :
  • Machine Learning
  • NLP
  • AI-ML-DS
  • Python-nltk
  • python
Practice Tags :
  • Machine Learning
  • python

Similar Reads

  • How to Parse a Time String Containing Milliseconds in Python?
    In many applications, precise timekeeping is crucial, and that often includes parsing time strings down to the millisecond. Python offers robust tools for handling date and time data, making this task straightforward. This article will guide us through the process of parsing time strings containing
    3 min read
  • Introduction to Python Dateutil Package
    The Dateutil is a Python package that enhances Python's built-in datetime module and makes it more flexible and user-friendly when dealing with dates and times. In this article, we will learn about the basics of the python-dateutil package, including its installation, key features, and practical exa
    3 min read
  • PHP | date_parse_from_format() Function
    The date_parse_from_format() is an inbuilt function in PHP which is used to get information about given date formatted according to the specified format. The date_parse_from_format() function accepts two parameters and returns associative array with detailed information about given date. Syntax: arr
    3 min read
  • DateFormat parse(string , ParsePosition) Method in Java with Examples
    DateFormat class of java.text package is an abstract class that is used to format and parse dates for any locale. It allows us to format date to text and parse text to date. DateFormat class provides many functionalities to obtain, format, parse default date/time. Note: DateFormat class extends Form
    3 min read
  • Formatting Dates in Python
    In different regions of the world, different types of date formats are used and for that reason usually, programming languages provide a number of date formats for the developed to deal with. In Python, it is dealt with by using a liberty called DateTime. It consists of classes and methods that can
    5 min read
  • JSTL Formatting <fmt:parseDate> Tag
    In JSTL, the formatting <fmt:parseDate> tag is used to parse the date strings into proper java.util.Date objects. This is useful if we need to represent a string in a formatted Date object. Using the tag the tasks of date parsing and formatting can be easily done in Java Server Pages applicati
    3 min read
  • Python - Extract date in String
    Given a string, the task is to write a Python program to extract date from it. Input : test_str = "gfg at 2021-01-04" Output : 2021-01-04 Explanation : Date format string found. Input : test_str = "2021-01-04 for gfg" Output : 2021-01-04 Explanation : Date format string found. Method #1 : Using re.s
    4 min read
  • Python - Validate String date format
    Given a date format and a string date, the task is to write a python program to check if the date is valid and matches the format. Examples: Input : test_str = '04-01-1997', format = "%d-%m-%Y" Output : True Explanation : Formats match with date. Input : test_str = '04-14-1997', format = "%d-%m-%Y"
    3 min read
  • MonthDay parse(CharSequence, DateTimeFormatter) method in Java
    The parse(CharSequence text, DateTimeFormatter formatter) method of the MonthDay class in Java is used to get an instance of MonthDay from a text string using a specific formatter. Syntax: public static MonthDay parse( CharSequence text, DateTimeFormatter formatter) Parameters: This method accepts t
    2 min read
  • Perl - Extracting Date from a String using Regex
    In Perl generally, we have to read CSV (Comma Separated Values) files to extract the required data. Sometimes there are dates in the file name like sample 2014-02-12T11:10:10.csv or there could be a column in a file that has a date in it. These dates can be of any pattern like YYYY-MM-DDThh:mm:ss or
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences