Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8791067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:47:33+00:00 2026-06-13T22:47:33+00:00

This is probably a very basic question but after reading documentation I still can’t

  • 0

This is probably a very basic question but after reading documentation I still can’t figure out how to do it…

I have two strings in Python that contain dates of unknown format. I don’t know what formats they are in, except I know that both are valid date-time expressions. For example, one of them might be in the ISO format and the other in some other format.

All I need is to be able to compare the dates. What’s the correct way to turn strings into appropriate date-time objects so that they can be compared?

thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:47:33+00:00Added an answer on June 13, 2026 at 10:47 pm

    The dateutil module has a date parser which can parse date strings in many formats.

    For example,

    In [13]: import dateutil.parser as parser
    
    In [14]: parser.parse("19970902T090000")
    Out[14]: datetime.datetime(1997, 9, 2, 9, 0)
    
    In [15]: import datetime as dt
    
    In [16]: now = dt.datetime.now()
    
    In [17]: now.isoformat()
    Out[18]: '2012-11-06T15:08:51.393631'
    
    In [19]: parser.parse('2012-11-06T15:08:51.393631')
    Out[19]: datetime.datetime(2012, 11, 6, 15, 8, 51, 393631)
    
    In [20]: parser.parse('November 6, 2012')
    Out[20]: datetime.datetime(2012, 11, 6, 0, 0)
    

    Note that some datetime strings can be ambiguous: 10-09-2003 could mean October 9 or September 10, for example. dateutil has parameters like dayfirst and yearfirst to handle this:

    In [21]: parser.parse("10-09-2003")
    Out[21]: datetime.datetime(2003, 10, 9, 0, 0)
    
    In [22]: parser.parse("10-09-2003", dayfirst = True)
    Out[22]: datetime.datetime(2003, 9, 10, 0, 0)
    
    In [23]: parser.parse("10-09-03", yearfirst = True)
    Out[23]: datetime.datetime(2010, 9, 3, 0, 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This probably is a very very basic question but i can't seem to find
This is probably a very simple question but I just can't seem to figure
I realize that this is probably a very basic question, but I have spent
Probably this is a very basic question for shell programmers. But suppose I have
Okay, this is probably a very basic question; but, I'm just getting back in
Problem : This is a probably a very basic question, but how do I
OK, this is probably very basic, but I can't find the problem. I'm trying
This is probably a very simple question so please forgive my ignorance, but can
This is probably a very basic question but I haven't found any detailed information
This is probably a very basic R question...and feel a bit bad about asking...but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.