datetime strptime format with code examples

The strptime() function in Python is used to convert a string representation of a date and time to a datetime object. The strptime() function takes two arguments: the string to be converted, and the format of the string. The format of the string is specified using special codes, called format codes, which are similar to the codes used in the strftime() function.

Here is an example of how to use the strptime() function to convert a string to a datetime object:

from datetime import datetime

date_string = "2022-01-15 12:00:00"
date_format = "%Y-%m-%d %H:%M:%S"

date_object = datetime.strptime(date_string, date_format)

print(date_object)

In this example, the string "2022-01-15 12:00:00" is being converted to a datetime object using the format "%Y-%m-%d %H:%M:%S". The %Y represents the year with century as a decimal number, %m is the month as a zero-padded decimal number, %d is the day of the month as a zero-padded decimal number, %H is the hour (24-hour clock) as a zero-padded decimal number and %M is the minute as a zero-padded decimal number, %S is the second as a zero-padded decimal number.

Here are some more examples of format codes that can be used with the strptime() function:

  • %A: Full weekday name
  • %B: Full month name
  • %w: Weekday as a decimal number, where Sunday is 0 and Saturday is 6
  • %d: Day of the month as a zero-padded decimal number
  • %Y: Year with century as a decimal number
  • %y: Year without century as a zero-padded decimal number

Here's an example using different format codes:

from datetime import datetime

date_string = "Mon,15 Jan 2022 12:00:00 PM"
date_format = "%a,%d %b %Y %I:%M:%S %p"

date_object = datetime.strptime(date_string, date_format)

print(date_object)

In this example, the string "Mon,15 Jan 2022 12:00:00 PM" is being converted to a datetime object using the format "%a,%d %b %Y %I:%M:%S %p". %a represents the abbreviated weekday name, %b is the abbreviated month name, %I is hour (12-hour clock) as a zero-padded decimal number, %p is either AM or PM.

It's important to note that the strptime() function is not very flexible, and it can be difficult to use when dealing with strings in a non-standard format. If you need to parse date strings with a non-standard format, you may want to use a library such as dateutil.

In conclusion, strptime() function is
In addition to the strptime() function, Python also provides the strftime() function, which is used to convert a datetime object to a string representation of the date and time. The strftime() function takes one argument: the format of the output string. The format of the output string is specified using the same format codes as the strptime() function.

Here is an example of how to use the strftime() function to convert a datetime object to a string:

from datetime import datetime

date_object = datetime(2022, 1, 15, 12, 0, 0)
date_format = "%Y-%m-%d %H:%M:%S"

date_string = date_object.strftime(date_format)

print(date_string)

In this example, the datetime object date_object is being converted to a string using the format "%Y-%m-%d %H:%M:%S".

It's also possible to create a datetime object from current date and time using datetime.now() and datetime.utcnow() method, the first one will give you the current date and time in your local time zone and the second one will give you the current date and time in UTC.

from datetime import datetime

# Current date and time in local time zone
local_time = datetime.now()

# Current date and time in UTC
utc_time = datetime.utcnow()

print("Local time :",local_time)
print("UTC time :",utc_time)

Another useful function is timedelta, which allows you to perform arithmetic with datetime objects. For example, you can use the timedelta function to add or subtract days, hours, minutes, etc. from a datetime object.

from datetime import datetime, timedelta

date_object = datetime(2022, 1, 15, 12, 0, 0)

# Adding 3 days to the date object
new_date = date_object + timedelta(days=3)
print(new_date)

# Subtracting 2 hours from the date object
new_date = date_object - timedelta(hours=2)
print(new_date)

In this example, the timedelta function is used to add 3 days to the datetime object date_object and to subtract 2 hours from the datetime object date_object.

In conclusion, Python provides a rich set of tools for working with dates and times, including the datetime, strptime(), strftime() and timedelta functions. These functions make it easy to convert between string representations of dates and times and datetime objects, as well as perform arithmetic with dates and times.

Popular questions

  1. What is the strptime() function in Python and what is it used for?

The strptime() function in Python is a method of the datetime class. It is used to convert a string representation of a date and time to a datetime object. The strptime() function takes two arguments: the first is the string representation of the date and time, and the second is the format of the string. The format of the string is specified using special codes, such as %Y for the year, %m for the month, and %d for the day.

  1. How do you use the strptime() function to convert a string to a datetime object?

To use the strptime() function to convert a string to a datetime object, you need to import the datetime module and then call the strptime() method on the datetime class. The first argument of the strptime() method is the string representation of the date and time, and the second argument is the format of the string. Here is an example:

from datetime import datetime

date_string = "2022-01-15 12:00:00"
date_format = "%Y-%m-%d %H:%M:%S"

date_object = datetime.strptime(date_string, date_format)

print(date_object)
  1. What are some examples of codes that can be used in the format string for the strptime() function?

There are several codes that can be used in the format string for the strptime() function. Here are a few examples:

  • %Y: The year with century as a decimal number.
  • %m: The month as a zero-padded decimal number.
  • %d: The day of the month as a zero-padded decimal number.
  • %H: The hour (24-hour clock) as a zero-padded decimal number.
  • %M: The minute as a zero-padded decimal number.
  • %S: The second as a zero-padded decimal number.
  1. How can you use the strftime() function to convert a datetime object to a string?

The strftime() function is used to convert a datetime object to a string representation of the date and time. The strftime() function takes one argument: the format of the output string. The format of the output string is specified using the same format codes as the strptime() function. Here is an example of how to use the strftime() function to convert a datetime object to a string:

from datetime import datetime

date_object = datetime(2022, 1, 15, 12, 0, 0)
date_format = "%Y-%m-%d %H:%M:%S"

date_string = date_object.strftime(date_format)

print(date_string)
  1. How can you use the timedelta function to perform arithmetic with datetime objects?

The timedelta function allows you to perform arithmetic with datetime

Tag

Datetime

Posts created 2498

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top