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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:09:38+00:00 2026-05-27T03:09:38+00:00

I use a statement as shown below to create a datetime object from a

  • 0

I use a statement as shown below to create a datetime object from a string:

t = datetime.strptime("0023-10-10", "%Y-%m-%d")

Later, somewhere in my code uses the t object and invoke the strftime method with the same format string:

t.strftime("%Y-%m-%d")

This causes a ValueError: year=23 is before 1900; the datetime strftime() methods require year >= 1900.

It seems that the validation of the %Y input is different in this two similar methods.
So I have to do the following to make sure I don’t accept some bad years like 23:

try:
    format = "%Y-%m-%d"
    t = datetime.strptime("0023-10-10", format)
    t.strftime(format)
except ValueError:
    ...

I wonder if there’s a better way to do this validation.

  • 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-27T03:09:39+00:00Added an answer on May 27, 2026 at 3:09 am

    I like your idea of using a try..except to validate the input, since in some future version of Python, years < 1000 might be acceptable.

    This comment in the code suggests this restriction is limited to Python’s current implementation of strftime.


    In Python 2.7, the exception occurs for years < 1900, but
    in Python 3.2, the exception occurs for years < 1000:

    import datetime as dt
    format = "%Y-%m-%d"
    t = dt.datetime.strptime("0023-10-10", format)
    try:
        t.strftime(format)
    except ValueError as err:
        print(err)
    

    prints

    year=23 is before 1000; the datetime strftime() methods require year >= 1000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I use a conditional statement targeting IE6 and below with some PHP code
i use this statement for a single where condition ('SELECT * FROM users WHERE
I can use the PRINT statement in a stored procedure to debug my code.
If I created a Linq statement as shown below, it works fine. var Jobs
I have created a stored procedure shown below ,how will i call this from
I have a code file from the boto framework pasted below, all of the
I always use If statement (In C#) as (1. Alternative); if (IsSuccessed == true)
i have been told to use 'when' statement to make multiplexer but not use
Given the following example, why do I have to explicitly use the statement b->A::DoSomething()
I use the following statement prepared and bound in ODBC: SELECT (CASE profile WHEN

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.