Python date and time

Python programs can handle dates and times in many ways, and converting date formats is a common feature.

Python provides a time and calendar module that can be used to format dates and times.

The time interval is a floating point fraction in seconds.

Each timestamp is expressed as how long it has been since midnight (epoch) on January 1, 1970.

The Python time module has a number of functions that convert common date formats. For example, the function time.time() is used to get the current timestamp, as shown in the following example:

Instance (Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- import time; # Introducing the time module ticks = time< Span class="hl-code">.time() print "The current timestamp is:", ticks

The output of the above example:

The current timestamp is:  1459994552.51

The timestamp unit is best for date operations. But the date before 1970 cannot be expressed by this. The date is too far away, UNIX and Windows only support 2038.



What is a time tuple?

Many Python functions use a set of 9 numbers of digital processing time:

serial number FieldValue
04 digits2008
1month1 to 12
2Day1 to 31
3hours0 to 23
4minutes0 to 59
5seconds0 to 61 (60 or 61 is a leap second)
6The day of the week0 to 6 (0 is Monday)
7The first few days of the year1 to 366 (Julian calendar)
8Daylight 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 numberpropertyvalue
0tm_year2008
1tm_mon1 to 12
2tm_mday1 to 31
3tm_hour0 to 23
4tm_min0 to 59
5tm_sec0 to 61 (60 or 61 is a leap second)
6tm_wday0 to 6 (0 is Monday)
7tm_yday1 to 366 (Julian calendar)
8tm_isdst-1, 0, 1, -1 is the flag that determines whether it is daylight saving time


Get current time

Converts from a timestamp that returns a floating point number to a time tuple, as long as the floating point number is passed to a function such as localtime.

Instance (Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- import time localtime = time< Span class="hl-code">.localtime(time.time( )) print "Local time is:", localtime

The output of the above example:

Get formatting time

You can choose from a variety of formats, but the easiest way to get a readable time pattern is asctime():

Instance (Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- import time localtime = time< Span class="hl-code">.asctime( time.localtime< /span>(time.time()) ) print "Local time is:", localtime

The output of the above example:

Local time is : Thu Apr 7 10:05:21 2016

Formatted date

We can use the time module's strftime method to format the date:

time.strftime(format[, t])

Instance demo:

Instance(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- import time # Formatted as 2016-03-20 11:45:39 form print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # Formatted as Sat Mar 28 22:24:24 2016 form print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) # Convert a format string to a timestamp a = "Sat Mar 28 22:24:24 2016" print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))
2016-04-07 10:25:09
Thu Apr 07 10:25:09 2016
1459175064.0

Time and date formatting symbols in python:






javacodegeeks is optimized for learning.© javacodegeeks
All Right Reserved and you agree to have read and accepted our term and condition.