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 979167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:10:39+00:00 2026-05-16T04:10:39+00:00

Like other questions asked here, im looking to do a simple conversion of time

  • 0

Like other questions asked here, im looking to do a simple conversion of time formats. I’ve found answers on how to do this in Perl, but not in Python.

I have a string like so:

on Jun 03, 02010 at 10:22PM

and I’d like to convert it to a datetime object like this:

Thu, 03 Jun 2010 22:22:00 -0000

I have sliced up my input like so:

date = str(comment.div.contents[5].contents)
month = date[6:9]
day   = date[10:12]
year  = date[15:19]
time  = date[23:30]

which sets me up with some nice variables I can throw back into datetime but before I so, I must convert the time. Should I divide up time in the example above and calculate the hh and {AM|PM} separately?

  • 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-05-16T04:10:39+00:00Added an answer on May 16, 2026 at 4:10 am

    Using dateutil:

    import datetime as dt
    import dateutil.parser as dparser
    
    date_str='on Jun 03, 02010 at 10:22PM'
    date=dparser.parse(date_str)
    print(date)
    # 2010-06-03 22:22:00
    print(date.strftime('%a, %d %b %Y %H:%M:%S'))
    # Thu, 03 Jun 2010 22:22:00
    

    If you can somehow strip out the pesky ‘on’ and change 02010 to 2010, in date_str then you could use dt.datetime.strptime:

    date_str='Jun 03, 2010 at 22:22PM'
    date=dt.datetime.strptime(date_str,'%b %d, %Y at %H:%M%p')
    print(date)
    # 2010-06-03 22:22:00
    

    Or, as Muhammad Alkarouri points out, if date_strs always start with on and use a four-digit year prefixed by zero, then you could use

    date_str='on Jun 03, 02010 at 22:22PM'
    date=dt.datetime.strptime(date_str,'on %b %d, 0%Y at %H:%M%p')
    print(date)
    # 2010-06-03 22:22:00
    

    Python’s strftime and strptime use your machine’s C functions of the same name. So check your local man page for what format codes are available for your machine. For a general list of format codes (which may be available) see https://www.php.net/strftime.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may seem like a stupid question, so here goes: Other than parsing the
This question would probably apply equally as well to other languages with C-like multi-line
I asked various questions about my problem ( here and here ) and I
This question was previously asked and answered correctly, but there did not seem to
I was expecting to find that section within System.Web.Configuration namespace just like other Web.config
Like any other hard disk, virtual hard discs (*.vhd) will suffer from fragmentation. So
Like every other web developer on the planet, I have an issue with users
Like many other SharePoint users, I've had to create a custom list definition. After
It appear that SQL Server like most other products Random Function really is not
I would like to hear other people's advice on when one should build a

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.