Onecore

  • Home
  • About
  • Advertise
  • Archives
  • Privacy Policy
  • Contact

Python Date and Time

software templates

Python can handle date and time on your system in multiple ways. Depending on your operating system the method to access the system time and date will vary. Code in this tutorial is tested on ubuntu linux and windows operating system.

At the end of the tutorial you’ll learn how to manipulate the dates and calendar using python. Some of the platform specific modules are also discussed in this tutorial.For date and calendar related programs, you can use of the modules explained in this tutorial as per your requirement. Feel free to let me know if there is anything missing in this tutorial or needs more explanation.

System Date

Open command prompt and type python and type the following code.

import time
temp_time = time.time()
print temp_time;

We have to import the time before using it. We take temp_time variable that holds the system time that we’re retriving. If you print the value of temp_time variable you’ll notice that it is not readable as per our regular time format.

We have to format the output in order to make it more readable.

from time import strftime
time.strftime("%Y-%m-%d %H:%M:%S")

We’re using strftime() function to format the the time. You’ll see the formated output. You can use %A if you want print the full name of the weekday. Similarly you can use %B for the full name of the month. In case of 12 hour clock output, you can use %I in your function.

Python Localtime

Localtime() prints much better readable output.
from time import localtime
localtime()

You’ll see each formated variable followed by “tm_” .

CPU Time

If you execute the code below you’ll get the CPU time as a floating point number in seconds.

time.clock();

This output maybe less clear but is more useful if you manipulate CPU time in your program.

Python datetime objects

The datetime module is from the standard library of python. You can use this on almost every platform as it’s part of standard library class datetime.

from datetime import datetime
datetime.now()

You’ll get current year, month, day, hour, minutes and seconds in this output.

Unix and Linux Time

You have to import time module from the standard python libraries time class. This output will be represented in the floating point numbers.

from time import time
time()

You’ll see the output of time() in floating point number.

System Calendar

Like system time you have to import calendar in order to use it in your code. We’ll see how to get the formated calendar in our output.

import calendar
cal=calendar.month(2012,1);
print "current month",cal;

If you execute the above code you’ll see that it prints out the calendar of january 2012 on your command prompt or IDLE.

Check Leap Year using Calendar Module

You can check if the particular year is leap year or not using the calendar module.

import calendar
calendar.isleap(2012);

See if the output is true or false. If it’s leap year then it’ll output true or else flase.

This completes our short tutorial on python system time and date. Feel free to post your comment and suggestions below.

software templates

Featured Articles

  • Sample Test Cases for Calendar App
  • Sample Test Cases for Dialog Box
  • Test Cases for Amazon Login Screen Page
  • Sample Test Cases for Online Editor
  • Sample Test Cases for Online Book Shopping

WPForms

ENDORSEMENT DISCLOSURE: In order for us to support our website activities, we may receive monetary compensation or other types of remuneration for our endorsement, recommendation, testimonial and/or link to any products or services from this website.

Copyright © 2025 . This website uses cookies to ensure you get the best experience on our website.