Python Date Formatting

Learning Python Date Formatting is another important step involved in mastering the popular programming language. Learn more here.

Python Date Formatting

Time between two date-times

from datetime import datetime
a = datetime(2016,10,06,0,0,0)
b = datetime(2016,10,01,23,59,59)
a-b

datetime.timedelta(4, 1)

(a-b).days

4

(a-b).total_seconds()

518399.0

Python Date Formatting: Outputting datetime object to string

Uses C standard format codes.

from datetime import datetime
datetime_for_string = datetime(2016,10,1,0,0)
datetime_string_format = '%b %d %Y, %H:%M:%S'
datetime.strftime(datetime_for_string,datetime_string_format)

Oct 01 2016, 00:00:00

Python Date Formatting: Parsing string to datetime object

Uses C standard format codes.

from datetime import datetime
datetime_string = 'Oct 1 2016, 00:00:00'
datetime_string_format = '%b %d %Y, %H:%M:%S'
datetime.strptime(datetime_string, datetime_string_format)

datetime.datetime(2016, 10, 1, 0, 0)

Converting Dates to Strings with strftime

Now that you know how to create Date and Time objects, let us learn how to format them into more readable strings.

To achieve this, we will be using the strftime method. This method helps us convert date objects into readable strings. It takes two parameters, as shown in the following syntax:

time.strftime(format, t)

The first parameter is the format string, while the second parameter is the time to be formatted, which is optional.

This method can also be used on a datetime object directly, as shown in the following example:

import datetime
 
x = datetime.datetime(2018, 9, 15)
 
print(x.strftime("%b %d %Y %H:%M:%S"))

Output:

Sep 15 2018 00:00:00

We have used the following character strings to format the date:

  • %b: Returns the first three characters of the month name. In our example, it returned “Sep”
  • %d: Returns day of the month, from 1 to 31. In our example, it returned “15”.
  • %Y: Returns the year in four-digit format. In our example, it returned “2018”.
  • %H: Returns the hour. In our example, it returned “00”.
  • %M: Returns the minute, from 00 to 59. In our example, it returned “00”.
  • %S: Returns the second, from 00 to 59. In our example, it returned “00”.

We did not pass a time, hence the values for time are all “00”. The following example shows how the time can be formatted as well:

import datetime
 
x = datetime.datetime(2018, 9, 15, 12, 45, 35)
 
print(x.strftime("%b %d %Y %H:%M:%S"))

Output

Sep 15 2018 12:45:35

How to unpack binary formatted dates in python?

Use the struct module to unpack these values to a tuple of integers:

import struct year, month, day = struct.unpack('>HBB', "\x07\xdb\x0c\x01")

The >HBB format specifier tells struct to expect 3 values in little endian format, one unsigned short (a 2 byte value, read as an integer) and to unsigned chars (1 byte values, read as integers).

You could then pass these to datetime.date() to create a date object if you want:

from datetime import date import struct somedate = date(*struct.unpack('>HBB', "\x07\xdb\x0c\x01"))

Demo:

>>> import struct >>> from datetime import date >>> date(*struct.unpack('>HBB', "\x07\xdb\x0c\x01")) datetime.date(2011, 12, 1)

or you could use string formatting:

>>> '{2:02d}-{1:02d}-{0}'.format(*struct.unpack('>HBB', "\x07\xdb\x0c\x01")) '01-12-2011'

to create a string representing that date.

More information on Python:

LEAVE A REPLY

Please enter your comment!
Please enter your name here