PythonPython3java
 

Python date and time

Python adventures can oversee dates and times from various perspectives, and changing over date clusters is a common part.

Python gives a period and timetable module that can be utilized to setup dates and times.

The time interim is a gliding point part in a split second.

Each timestamp is conferred concerning what degree it has been since midnight (age) on January 1, 1970.

The Python time module has diverse limits that convert fundamental date gatherings. For example, the limit time.time() is used to get the current timestamp, as showed up in the going with 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 2019

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 2019-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 2019 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 2019" print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))
2019-04-07 10:25:09
Thu Apr 07 10:25:09 2019
1459175064.0

Time and date formatting symbols in python:






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