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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:41:29+00:00 2026-06-13T15:41:29+00:00

I need a reliable way to convert formatted date strings into datetime objects so

  • 0

I need a reliable way to convert formatted date strings into datetime objects so I can save into the database. I’m importing data from an RSS feed and the date strings look like this 10/31/2012 11:59:00 PM, but every once in a while one will come through with no time data, like this: 9/24/2012. Is there a way to convert the string to a datetime object regardless if the time is present or not?

I was using Date.strptime() because Date.parse() wasn’t being reliable enough for me. Here’s what I was using, which will error out when it encounters the date string without the time.

Date.strptime(i.xpath('activedate').text, '%m/%d/%Y %I:%M:%S %p').to_datetime

What would be the best way to handle this?

  • 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-13T15:41:30+00:00Added an answer on June 13, 2026 at 3:41 pm

    I recommend chronic:

    require 'chronic'
    
    Chronic.parse '10/31/2012 11:59:00 PM'
    #=> 2012-10-31 23:59:00 0800
    Chronic.parse '9/24/2012'
    #=> 2012-09-24 12:00:00 0800
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using JavaScript Date object to convert milliseconds into a readable minutes:seconds formatted
I need to convert a database column in a (MSSQL) database table into UNIX
I need to know a reliable way I can determine the folder that a
I need a reliable way to detect how many CPU cores are on a
I really need to find a reliable way in order to store a web
Just curious whether it is reliable way to do following: I need to get
I need a robust/reliable/elegant way to handle any error conditions while re-loading firewall rules,
Does anyone know of a reliable way to store data to use across pages
I need to determine programmatically if the database supports the Geography data type and
What's the standard, most reliable way of doing this? I need to redirect 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.