Python programs can handle dates and times in many ways, and converting date formats is a common function.
Python provides a time and calendar module that can be used to format dates and times.
The time interval is a floating point decimal number in ** seconds. **
Each timestamp is expressed in terms of how long has passed since midnight (epoch) on January 1, 1970.
There are many functions under Python's time module to convert common date formats. For example, the function time.time() is used to obtain the current timestamp, as in the following example:
#! /usr/bin/python3
import time; #Introduce the time module
ticks = time.time()print("The current timestamp is:", ticks)
The output of the above example:
The current timestamp is: 1459996086.7115328
The time stamp unit is most suitable for date calculations. But dates before 1970 cannot be expressed in this way. Dates that are too far away will not work, UNIX and Windows only support until 2038.
# Timing
import time
start_time = time.time()print('training took %fs!'%(time.time()- start_time))
The unit is ms
Many Python functions use 9 sets of numbers assembled in one element to process time:
Serial number | field | value |
---|---|---|
0 | 4 Digit Year | 2008 |
1 | Month | 1 to 12 |
2 | Day | 1 to 31 |
3 | Hours | 0 to 23 |
4 | Minutes | 0 to 59 |
5 | Seconds | 0 to 61 (60 or 61 is a leap second) |
6 | Day of the week | 0 to 6 (0 is Monday) |
7 | Day of the year | 1 to 366 (Julian calendar) |
8 | Daylight saving time | -1, 0, 1, -1 is the flag that determines whether it is daylight saving time |
The above is the struct_time tuple. This structure has the following properties:
Serial number | attribute | value |
---|---|---|
0 | tm_year | 2008 |
1 | tm_mon | 1 to 12 |
2 | tm_mday | 1 to 31 |
3 | tm_hour | 0 to 23 |
4 | tm_min | 0 to 59 |
5 | tm_sec | 0 to 61 (60 or 61 is a leap second) |
6 | tm_wday | 0 to 6 (0 is Monday) |
7 | tm_yday | Day of the year, 1 to 366 |
8 | tm_isdst | Whether it is daylight saving time, the values are: 1 (daylight saving time), 0 (not daylight saving time), -1 (unknown), default -1 |
To convert from a timestamp that returns a floating point number to a time tuple, just pass the floating point number to a function such as localtime.
#! /usr/bin/python3
import time
localtime = time.localtime(time.time())print("Local time is:", localtime)
The output of the above example:
The local time is: time.struct_time(tm_year=2016, tm_mon=4, tm_mday=7, tm_hour=10, tm_min=28, tm_sec=49, tm_wday=3, tm_yday=98, tm_isdst=0)
You can choose various formats according to your needs, but the simplest function to get a readable time mode is asctime():
#! /usr/bin/python3
import time
localtime = time.asctime( time.localtime(time.time()))print("Local time is:", localtime)
The output of the above example:
Local time is: Thu Apr 710:29:132016
We can use the strftime method of the time module to format the date:
time.strftime(format[, t])
#! /usr/bin/python3
import time
# Formatted as 2016-03-2011:45:39 form
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
# Format as Sat Mar 2822:24:242016 form
print(time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))
# Convert format string to timestamp
a ="Sat Mar 28 22:24:24 2016"print(time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y")))
The output of the above example:
2016-04-0710:29:46
Thu Apr 0710:29:4620161459175064.0
Time and date formatting symbols in python:
The Calendar module has a wide range of methods for processing annual and monthly calendars, such as printing a monthly calendar:
#! /usr/bin/python3
import calendar
cal = calendar.month(2016,1)print("The following outputs the calendar for January 2016:")print(cal)
The output of the above example:
The following outputs the calendar for January 2016:
January 2016
Mo Tu We Th Fr Sa Su
12345678910111213141516171819202122232425262728293031
Reference: https://www.runoob.com/python3/python3-date-time.html