Time and date string conversion in Python

Dates and times can be conveniently represented as strings in many data formats. However, in order to perform arhithmetic operations on these dates and times (like finding time differences, adding or subtracting time, etc.), In order to use them, we must first transform them into a datetime object.

REST APIs are a popular source of datetimes in string format, which can be easily converted to other formats.

Timezones are another potential source of frustration when dealing with datetime objects, so we'll have to keep that in mind during conversion.

We'll look at the Python built-in datetime module as well as third-party modules like dateutil, arrow, and Maya to see how to convert a string date/time into a datetime object with support for multiple timezones.

Dates, times, and datetimes are the three object types that make up the datetime module. Date is stored in the date object, time in the time object, and both in the datetime object.

import datetime print ( Date and time now: f' {datetime datetime now()} ' )

If this program was run, it would cause:

Date and time now: December 1st, 2022 10:27:03 929149

In the absence of any specific instructions to the contrary, strings are always presented in their default format, i.e. e "2022-12-01 10:27:03" as a formatted string ISO 8601 dates are formatted as "929149" (YYYY-MM-DDTHH:MM:SS). mmmmmm) You can easily parse to a datetime object if the string we use to create it is in the same ISO 8601 format, or if you know the format you'll be receiving in advance.

import string date_time_str = datetime '2022-12-01 10:27:03 929149' object_datetime = datetime datetime strptime(date_time_str, '%Y-%m-%d%H:%M:%S %f' ) print ( 'Date:' and date_time_obj date()) print ( 'Time:' , TimeDateObject time()) print ( 'Date-time:' , time_date_object

When you run it, it will show you the current time and date:

Date: 2022-12-01 Time: 10:27:03 929149 As of 10:27:03 UTC on December 1, 2022. 929149

The strptime() function is called with two arguments:

  • The date, in string format
  • The structure of the initial case

Because datetime doesn't have to guess how to interpret the format, parsing is much faster when the format is specified beforehand. A datetime value will be returned.

The example given here is: "2022-12-01 10:27:03" Input string "929149" is formatted as "%Y-%m-%d%H:%M:%S" This date string uses the %f" format. Datetime value is returned and saved in date_time_obj.

The date() and time() methods are accessible on this object directly because it is a datetime object. The output clearly displays the 'date' and 'time' components of the input string.

Using Tokens as a Format

Learn the "%Y-%m-%d%H:%M:%S" format tokens; they're useful. %f" previously

Each token in the date/time string stands for a specific aspect of the date/time, such as the day of the month, year, month, etc. There is a wide variety of formatting options available thanks to the extensive list of supported tokens. Common ones include those we've used before, as well as those that are used frequently:

  • 4 digit year (%Y)
  • %m: Month
  • Number of the day in the month
  • The %H notation represents the 24-hour time period.
  • Amount of Time in Minutes
  • Percentage of Time in Seconds
  • Number of microseconds
icon-information-circle-solid.svg

Aside from the year, it is assumed that all of these tokens will be zero-padded (i.e. e Zeros are added to the end of the number 8 (August) to make it a valid month number.

To convert a string to a datetime object, use strptime() if you know its format. Let's take a look at a non-trivial format conversion example:

import timestamp timestr = 'Jul 17 2022 9:20AM' The formula date_time_obj = datetime datetime strptime(date_time_str, '%b%d%Y%I:%M%p' ) print ( 'Date:' and date_time_obj date()) print ( 'Time:' , TimeDateObject time()) print ( 'Date-time:' ., date_time_obj)

July 17, 2022, 9:20 a.m. was the only valid format for the input string. Based on our familiarity with this format, we were able to convert the data to a datetime object by mapping the individual components to the ISO 8601 format:

Date: 2022-07-17 Time: 09:20:00 The time now is 07-17-2022 09:20:00.

Standard datetime strings and their strptime() representations are as follows.

For example, "June 28, 2018, 7:40AM" can be formatted as "%b%d%Y at%I:%M%p." The format for "September 18, 2017, 22:19:55" is "%B%d,%Y,%H:%M:%S." For example, "Sun,05/12/99,12:30PM" would become "%a,%d/%m/%y,%I:%M%p." Date: Monday, March 21, 2015 Format: %a, %d, %B, %Y Date Format: "2018-03-12T10:12:45Z" to "%Y-%m-%dT%H:%M:%SZ"

As long as you use the appropriate set of format tokens, you can parse a date-time string in any format.

Managing time zones adds another layer of complexity to dealing with dates and times. All preceding examples have ignored the time zone. Naive datetime objects are what you get when you combine these two concepts.

Datetime objects, however, have a special field—tzinfo—designed to hold information about time zones.

import datetime as dt dtime = dt datetime now() print (dtime) print (dtime tzinfo)

It is expected that the tzinfo field will contain a datetime. Time Zone Data Object Its default value of None indicates that the datetime object does not take the user's time zone into account. Pytz is a popular third-party library used to manage time zones. The tzinfo field also accepts PyTz objects.

Get it here if you don't have it already:

I installed pytz with pip.

With PyTz, we can set a reference for dates and times that take into account time zones, such as UTC:

import datetime as dt import dtime = pytz datetime now(pytz utc) print (dtime) print (dtime tzinfo)

Output:

2022-12-01 02:07:41 960920+00:00 UTC

Due to the timezone adjustment, it is now 2 a.m. instead of 11 a.m. The time and date are now displayed in the new time zone.

There is a one hour discrepancy between the shown time and UTC, the primary reference for synchronizing events around the world. We've chosen UTC as our time standard, making the offset 00:00. Here we have an item that can tell you the time in other time zones.

Similarly, we can change how different timezones interpret the same date and time. Here's how to format a string like "2022-06-29 17:08:00" as a datetime in the "America/New_York" timezone:

import datetime as dt import the pytz date_time_str = '2022-06-29 17:08:00' When: date_time_obj = dt datetime strptime(date_time_str, '%Y-%m-%d%H:%M:%S' ) UTC equivalent: pytz timezone( 'America/New_York' ) date_time_zone_obj = date_time_zone localize(date_time_obj) print (timezone_date_time_obj) print (timezone_date_time_obj tzinfo)
icon-information-circle-solid.svg

Note that localization converts a datetime that does not account for timezones to one that does and uses that timezone instead of the current one. This means that the date and time remain unchanged, but the time it represents has shifted due to the change in time zone.

Same datetime value, but shifted backwards by four hours relative to UTC:

2022-06-29 17:08:00-04:00 America/New_York

Since 17:08 in New York is not the same as 17:08 in Tokyo, It's 3:08 in the afternoon in New York at 17:08 in Tokyo.

Where can I look up all of the various names and abbreviations for time zones?

Examine the all_timezones field to see a complete list of time zones:

print ( "There are" { len (pytz all_timezones)} PyTzn timezones. ) for time_zone in pytz all_timezones: print (time_zone)

View our hands-on, practical Git tutorial, complete with best practices, industry standards, and a handy cheat sheet. Don't just Google "Git commands;" instead, take the time to learn the language.

PyTz includes 594 timezones. Africa/Abidjan Africa/Accra Africa/Addis_Ababa Africa/Algiers Africa/Asmara Africa/Asmera

Instead of forcing a timezone-naive datetime through the filter of a specific timezone, we can convert the timezone of a timezone-aware datetime object.

This is distinct from localization because localization refers to a specific moment in time, while converting the timezone of an object refers to that same moment in time as seen through a different lens:

import datetime as dt import Python Time Zone: pytz Time Zone: pytz timezone( 'America/New_York' ) new_datetime_object = dt datetime time_in_london @ now(timezone_nw) = pytz timezone( 'Europe/London' ) London's DateTime Object Equals New York's DateTime Object astimezone(timezone_london) print ( 'America/New_York:' , obj_nw_datetime) print ( 'Europe/London:' (, london_datetime_obj)

A single datetime object was initialized with the current time and the "America/New_York" timezone. After that, we've changed the timezone to "Europe/London" using the astimezone() function. Since UTC offset is used as a connecting factor, the two datetimes will output different values:

UTC/GMT: 2022-11-30 21:24:30 America/New_York 123400-05:00 Time Zone (Europe/London): 2022-12-01 02:24:30 123400+00:00

Since London is 5 hours ahead of New York, at 2:24 on the following day it will be the same time there as it will be in New York at 21:24 the day before.

As might be expected, there is a 5-hour discrepancy between the dates and times.

The datetime module in Python allows for the easy conversion of any string to a datetime object. Creating the proper formatting code string that strptime() can understand is the main issue, though. Time is wasted creating this string, and it makes the code less readable.

To simplify matters, we can instead rely on other, external libraries.

Sometimes these external libraries have better in-built support for manipulating and comparing dates and times, and sometimes they even have timezones built-in so you don't need to include an additional PyTz package.

In the following sections, we'll examine several of these libraries.

Extension of the datetime module, dateutil To parse a string, we benefit from not having to pass any parsing code.

Python's dateutil allows you to easily convert strings to datetime without the need for a format token by performing the following steps:

from dateutil parser import parse parse("datetime"), "time"); '2018-06-29 22:21:41' ) print (datetime)

The string is automatically parsed by this parse function. Do not worry about including a format string. Let's use dateutil to parse some sample strings of varying formats:

from dateutil parser import date_array = [ parse '2018-06-29 08:15:27 243860' , 'Jun 28 2018 7:40AM' , The date and time shown is "June 28, 2018 at 7:40AM." , Date and time: "September 18th, 2017 22:19:55" , 'Sun, 05/12/1999, 12:30PM' , Date: "Monday, March 21, 2015" , '2018-03-12T10:12:45Z' , '2018-06-29 17:08:00 586525+00:00' , '2018-06-29 17:08:00 586525+05:00' , Date and Time: "Tuesday, September 6, 2017, at 4:30 p.m." ] for date in date_array: print ( To "Parse" (+ date) dt = parse(date) print (dt date()) print (dt time()) print (dt tzinfo) print ( '\n' )

Output:

Processing time: 6/29/2018 8:15:27 243860 2018-06-29 08:15:27 243860 None Date parsed: June 28, 7:40 a.m. 2018-06-28 07:40:00 None Time of parsing: 7:40AM, June 28, 2018 2018-06-28 07:40:00 None Input date/time: September 18, 2017, 11:19:55 2017-09-18 22:19:55 None Time of parsing: May 12, 1999 at 12:30 PM 1999-05-12 12:30:00 None Date of Parsing: March 21st, 2015 2015-03-21 00:00:00 None Processed at: 2018-03-12 10:12:45Z 2018-03-12 10:12:45 tzutc() Time of parsing: 6/29/2018 7:08:00 586525+00:00 2018-06-29 17:08:00 586525 tzutc() Time of parsing: 6/29/2018 7:08:00 586525+05:00 2018-06-29 17:08:00 586525 A zero offset of 18000 seconds is tzoffset(None). Date and time of dissection: September 6, 2017, 4:30 p.m. 2017-09-06 16:30:00 None

The dateutil module demonstrates how simple it is to parse almost any kind of string.

While this is helpful, it's worth remembering that predicting the format slows down the code significantly, so if your code needs high performance, this might not be the best approach.

Parsing strings and switching time zones are two other tasks made simple with Maya. Python's Maya provides a convenient method for converting strings.

import dt = maya parse( '2018-04-29T17:45:25Z' ) datetime() print (dt date()) print (dt time()) print (dt tzinfo)

Output:

2018-04-29 17:45:25 UTC

Time zone converters typically consist of the following:

import It can be written as: maya dt = maya parse( '2018-04-29T17:45:25Z' ) datetime(to_timezone= 'America/New_York' , naive= False ) print (dt date()) print (dt time()) print (dt tzinfo)

Output:

2018-04-29 13:45:25 America/New_York

Isn't it convenient that it works so effortlessly now? Let's give maya a shot by feeding it the same strings we used for dateutil:

import date_array in maya = [ '2018-06-29 08:15:27 243860' , 'Jun 28 2018 7:40AM' , 'June 28, 2018, 7:40AM' , Time Stamp: "2017-09-18 22:19:55" , 'Sun, 05/12/1999, 12:30PM' , Today is Monday, March 21, 2015 , '2018-03-12T10:12:45Z' , '2018-06-29 17:08:00 586525+00:00' , '2018-06-29 17:08:00 586525+05:00' , Date and Time: "Tuesday, September 6, 2017, at 4:30 p.m." ] for date in date_array: print ( To "Parse" dt = maya (date+time) parse(date) datetime() print (dt)

Output:

Date parsed: June 29, 2018 8:15:27 243860 2018-06-29 08:15:27 243860+00:00 Time of parsing: 7:40AM, June 28, 2018 2018-06-28 07:40:00+00:00 Sunday, June 28, 2018 7:40AM EST Parsing Time 2018-06-28 07:40:00+00:00 Time of parsing: 9/18/2017 22:19:55 2017-09-18 22:19:55+00:00 Date of Parsing: May 12, 1999 at 12:30 PM 1999-05-12 12:30:00+00:00 Date of Parsing: March 21st, 2015 2015-03-21 00:00:00+00:00 Processing time: 2018-03-12 10:12:45Z 2018-03-12 10:12:45+00:00 Time of parsing: 6/29/2018 7:08:00 586525+00:00 2018-06-29 17:08:00 586525+00:00 Date/time of parsing: June 29th, 2018 17:08:00 586525+05:00 2018-06-29 12:08:00 586525+00:00 Day of Parsing: Tuesday, September 6, 2017, 4:30 p.m. 2017-09-06 16:30:00+00:00

All date formats were correctly parsed as you can see.

If we don't specify a timezone, it defaults to Coordinated Universal Time (UTC). So, if the time is not in UTC, we need to remember that we need to supply the to_timezone and naive parameters.

In Python, there is the Arrow library for working with dates and times. Just like in Maya before, it intelligently determines the proper date and time format. The interpreted arrow object yields a Python datetime object.

The arrow function in Python makes it simple to convert a string to a datetime:

import Axis dt = Axis get( '2018-04-29T17:45:25Z' ) print (dt date()) print (dt time()) print (dt tzinfo)

Output:

2018-04-29 17:45:25 tzutc()

The to() method of arrow makes it easy to switch between different time zones.

import formula: dt > arrow get( '2018-04-29T17:45:25Z' ) to( 'America/New_York' ) print (dt) print (dt date()) print (dt time())

Output:

2018-04-29T13:45:25-04:00 2018-04-29 13:45:25

The time zone specifier "America/New_York" has been appended to the date and time string.

Let's go ahead and use the same string set we did up top again:

import straight date_array = [ '2018-06-29 08:15:27 243860' , '2018-03-12T10:12:45Z' , '2018-06-29 17:08:00 586525+00:00' , '2018-06-29 17:08:00 586525+05:00' , ] for date in dates: dt = arrow get(date) print ( To "parse" + date) print (dt)

Over half of our examples involve date-time strings that have been commented out, which will cause this code to fail. For all other strings, the result will be:

Start of parsing: June 29th, 2018 8:15:27 243860 2018-06-29T08:15:27 243860+00:00 Time of Parsing: 2018-03-12T10:12:45Z 2018-03-12T10:12:45+00:00 Processed at: 6/29/2018 17:08:00 586525+00:00 2018-06-29T17:08:00 586525+00:00 Time taken for parsing: 6/29/2018 7:08:00 586525+05:00 2018-06-29T17:08:00 586525+05:00

The commented-out date-time strings cannot be parsed by the library without the appropriate format tokens being passed to it as hints.

Conclusion

Several methods for converting a string to a datetime object in Python are demonstrated in this article. You can use the standard Python datetime library or one of the many third-party libraries available, some of which are discussed here.

The default datetime package has the major flaw of requiring us to manually specify the parsing code for almost all date-time string formats. Therefore, you will likely need to modify your code if your string format evolves in the future. It is handled automatically by many third-party libraries, including those we've listed here.

Time zone differences present yet another challenge. In order to deal with them properly, it is recommended that you always keep the time in UTC format in your database and then convert it to the user's local timezone when it is needed.

You can use these libraries for more than just string parsing; they also work well with dates and times in a wide variety of contexts. I suggest reading the docs to get a better understanding of the features.

Convert Chinese Yuan to United States Dollar
Convert Chinese Yuan to United States Dollar

If you're thinking about taking a trip to the United States, you might consider exchanging some of your money into U.S. dollars, which is the official currency of the country. The international symbol for the currency is USD.USD is also the official currency in a few other countries, including Ecuador

Author: Dranky Cowell Author: Dranky Cowell
Posted: 2023-08-07 00:02:33
Chinese Yuan to United States Dollar Conversion: CNY to USD
Chinese Yuan to United States Dollar Conversion: CNY to USD

If you're considering a journey to the United States, it might be beneficial to convert some of your money into U.S. dollars, which is the official currency of the country. The internationally recognized symbol for this currency is USD.Additionally, USD serves as the official currency in Ecuador and El

Author: Dranky Cowell Author: Dranky Cowell
Posted: 2023-08-07 00:02:20
Convert Decimal to Inches with an Inch Fraction Calculator
Convert Decimal to Inches with an Inch Fraction Calculator

Utilize our inch-to-fraction calculator to effortlessly perform conversions between inch fractions, decimal values, metric measurements, and feet. Effective Techniques for Calculating Inch FractionsInches can be represented as fractions or decimals. When dealing with inch fractions, it is vital to

Author: Dranky Cowell Author: Dranky Cowell
Posted: 2023-08-06 00:13:21
Kilowatt to British Thermal Unit (International)/hour Conversion
Kilowatt to British Thermal Unit (International)/hour Conversion

Please enter the necessary values below to convert kilowatts [kW] to British thermal units per hour [Btu/h], or the other way around.Description: A kilowatt (symbol: kW) is a unit of power within the International System of Units (SI). The watt, after the Scottish inventor James Watt, serves as the base

Author: Dranky Cowell Author: Dranky Cowell
Posted: 2023-08-06 00:12:10
Showing page 1 of 13

VyConvert - since 2022
, US

Facebook| | DMCA

Gen in 0.0696 secs