![]() Similarly, we can switch the same datetime's interpretation between time zones. We've set the time to be in UTC, so the offset is 00:00. +00:00 is the difference between the displayed time and the UTC time as the global coordination anchor. It's no longer 11AM, but 2AM, because we've set the timezone a few hours back! This changes the timezone of the datetime. Using PyTz, we can create an anchor for time-zone aware datetimes, such as UTC: import datetime as dt If you don't have it already - install it via: $ pip install pytz You can set PyTz objects as the tzinfo field too. A very common external library for handling timezones is pytz. It's None by default, and denotes that the datetime object is timezone-naive. The tzinfo field is meant to be a datetime.timezone object, denoting the timezone information. However, the datetime objects contain a field exactly for storing timezone-related data - tzinfo: import datetime as dt These are known as naive datetime objects. All above examples so far are naive to the timezone. Handling date-times becomes more complex while dealing with timezones. Convert String to Datetime with Time Zones You can parse a date-time string of any format - as long as you use the correct string of format tokens for the input you're receiving. Here's a short list of common string-formatted datetimes and their corresponding formats for strptime(): "at 7:40AM" -> "%b %d %Y at %I:%M%p" ![]() Knowing this format, we mapped the constituent elements to the ISO 8601 format and converted it to a datetime object: Date: The input string was of one format - "9:20AM". Let's take a look at a non-trivial example that translates from one format to another: import datetimeĭate_time_obj = (date_time_str, '%b %d %Y %I:%M%p') If the format of a string is known, it can be easily parsed to a datetime object using strptime(). Using strptime() Format Tokens to Convert String to Different Datetime Format August is the 8th month, and is zero-padded to 08). ![]() Note: All of these tokens, except the year, are expected to be zero-padded (i.e. Some of the commonly used ones, that we've also used earlier are: The list of supported tokens is extensive enough to enable various formatting. ![]() It's worth taking a moment to understand format tokens - the "%Y-%m-%d %H:%M:%S.%f" from before.Įach token represents a different part of the date-time, like day, month, year, day of month or week, etc. As you can see from the output, it prints the 'date' and 'time' part of the input string! Format Tokens Since this is a datetime object, we can call the date() and time() methods directly on it. The returned datetime value is stored as date_time_obj. In our example, " 10:27:03.929149" is the input string and "%Y-%m-%d %H:%M:%S.%f" is the format of our date string. The return value is of the type datetime. Specifying the format like this makes the parsing much faster since datetime doesn't need to try and interpret the format on its own, which is much more expensive computationally. Here, we use the strptime() method, which accepts two arguments: Running it will print the date, time, and date-time: If our input string to create a datetime object is in the same ISO 8601 format or if you know the format you'll be receiving upfront, we can easily parse it to a datetime object: import datetimeĭate_time_str = ' 10:27:03.929149' # strptime(input_string, input_format)ĭate_time_obj = (date_time_str, '%Y-%m-%d %H:%M:%S.%f') When no custom formatting is given, the default string format is used, i.e. Running this code would result in: Current date/time: 10:27:03.929149 The date object holds the date, time holds the time, and datetime holds both date and time! import datetime The datetime module consists of three different object types: date, time, and datetime. In this guide - we'll take a look at how to convert a string date/time into a datetime object in Python, using the built-in datetime module, but also third-party modules such as dateutil, arrow and Maya, accounting for time zones. One of the most common sources of string-formatted datetimes are REST APIs that return agnostic strings, that we can then convert to other formats.Īdditionally - time zones are a common headache when it comes to working with datetime objects, so we'll need to think about that while converting too. However, to work with these dates and times in an arithmetic fashion (such as calculating time differences, adding or removing time, etc.) - we need to convert them to a datetime object. Data can be represented in various forms - and a convenient way to represent dates and times are strings.
0 Comments
Leave a Reply. |