Python Date and Time

Python Date and Time: Python can handle date and time in several ways. The calendar modules and time help track dates and times.

How To Get Python Date & Time?

Tick

Time intervals are floating-point numbers in units of seconds. Particular instants in time are expressed in seconds since 12:00 am, January 1, 1970 (epoch).

There is a popular time module available in Python which provides functions for working with times, and for converting between representations. The function time.time() returns the current system time in ticks since 12:00am, January 1, 1970(epoch).



Python Date and Time

Example:

import time;  
# This is required to include a time module. ticks = time.time() 
print "Number of ticks since 12:00am, January 1, 1970:", ticks

Output:

Number of ticks since 12:00 am, January 1, 1970: 7186862.73399

Time Tuple

Python function handles time as a tuple of 9 numbers. check here as follows:

Index Field Values
  0 4-digit year 2019
1 Month 1 to 12
  2 Day 1 to 31
3   Hour 0 to 23
 4 Minute 0 to 59
5   Second 0 to 61 (60 or 61 are leap-seconds)
6 Day of Week 0 to 6 (0 is Monday)
7 Day of year 1 to 366 (Julian day)
8 Daylight savings -1, 0, 1, -1 means library determines DST
Index Attributes Values
  0 tm_year 2019
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 are leap-seconds)
6 tm_wday 0 to 6 (0 is Monday)
7 tm_yday 1 to 366 (Julian day)
8 tm_isdst -1, 0, 1, -1 means library determines DST

Get Current Time

If you want to get instant time from seconds, while the epoch floating point value into a time tuple, pass floating point value to a function. (i.e local time)

Example:

import time; 
local time = time.localtime(time.time()) 
print "Local current time:", localtime

Output:

Local current time: time.struct_time(tm_year=2019, tm_mon=1,tm_mday=1, tm_hour=01, tm_min=20, tm_sec=3, tm_wday=2, tm_yday=198, tm_isdst=0)

Get formatted time

You can format any time as per your requirement. You can do it by asctime( )

Example:

import time; 
localtime = time.asctime( time.localtime(time.time()) ) 
print "Local current time:", localtime

Output:

Local current time: Tue Jan 13 10:17:09 2009

Get Calendar of Month

If you want to get a calendar of the month.

Example:

import calendar 
Cal = calendar.month(2008, 1) 
print "Here is the calendar:" 
print cal

Output:

Here is the calendar:

January 2019
Mo Tu We Th Fr Sa Su
1   2     3   4    5  6
7    8   9    10  11  12 13
14   15  16   17 18 19 20
21  22 23   24 25 26 27
28  29  30  31

Get date from a timestamp

We can also create date objects from a timestamp. A Unix timestamp is the number of seconds between a particular date and January 1, 1970, at UTC. And we can convert a timestamp to date using from timestamp() method.

Example

from datetime import date
timestamp = date.fromtimestamp(1326244364)
print("Date =", timestamp)

Output
Date = 2012-01-11

Print today’s year, month and day

Example

from datetime import date
#date object of today's date
today = date.today() 
print("Current year:", today.year)
print("Current month:", today.month)
print("Current day:", today.day)

Output
Current year: 2019
Current month: 5
Current day: 25

datetime.datetime

The datetime module has a class named dateclass that can contain information from both date and time objects.

Example

from datetime import datetime
#datetime(year, month, day)
a = datetime(2019, 11, 28)
print(a)
# datetime(year, month, day, hour, minute, second, microsecond)
b = datetime(2010, 5, 28, 23, 55, 59, 342380)
print(b)

Output
2019-11-28 00:00:00
2019-05-28 23:55:59.342380

Handling timezone in Python

If you need to display date and time based on their timezone. Rather than trying to handle timezone yourself, we suggest you use a third-party pytZ module.

Example

from datetime import datetime
import pytz
local = datetime.now()
print("Local:", local.strftime("%m/%d/%Y, %H:%M:%S"))
tz_NY = pytz.timezone('America/New_York') 
datetime_NY = datetime.now(tz_NY)
print("NY:", datetime_NY.strftime("%m/%d/%Y, %H:%M:%S"))
tz_London = pytz.timezone('Europe/London')
datetime_London = datetime.now(tz_London)
print("London:", datetime_London.strftime("%m/%d/%Y, %H:%M:%S"))

Output
Local time: 2018-12-20 13:10:44.260462
America/New_York time: 2018-12-20 13:10:44.260462
Europe/London time: 2018-12-20 13:10:44.260462