Python Date and Time (JNNC Technologgies)


There are various ways Python supplies date and time feature to add in program. Python’s Time and calendar module helps in tracking date and time. Also the ‘datetime’ provide classes for controlling date and time by both simple and complex ways.


There are two different date and time objects. These are:
  • naive
  • aware
The differences between ‘naive’ and ‘aware’ objects are:
  1. The ‘aware’ object holds the details about time-zone and daylight saving information whereas naïve objects do not have that feature.
  2. Naive objects are easy to understand and to work with.

Defining Tick

As we all can make an idea that time intervals have to be represented in floating point number. The floating-point numbers in units of seconds are signified by Tick in python. Particular instants of time are represented in seconds since 12:00 am, 1st of January, of the year 1990. A popular module name ‘time’ is available which provides functions that let’s programmer work with time and made conversion between time-representation possible.
The pre-defined function time.time() is used to return the current time of the system.
The daytime module exports the constants listed below:
  • MINYEAR
  • MAXYEAR
Two programs are shown showing the use of two different modules:
Example:
#!/usr/bin/python

import time;

ticktock = time.time();

print("Number of ticks:", ticktock)
Output:
Number of ticks: 1465927104.951356
Another program showing datetime module:
Example:
#!/usr/bin/python

from datetime import datetime

presentime = datetime.now()

print(" NOW THE TIME IS:", presentime)

print("Todays Date is:", presentime.strftime('%Y-%m-%d') )

print("Year is:", presentime.year)

print("MOnth is:", presentime.month)

print("Day is:", presentime.day)

0 Comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();