Python is an object-oriented programming language ideally designed for rapid prototyping of complex applications. Like Perl, Python source code is also available under the GNU General Public License (GPL).

In this article we will see about Python Calender, Date & Time with examples.

1) Get current date/time in Python

import time
import datetime

print datetime.datetime.now()
Output: Output will be something like this:

2017-09-28 18:20:58.244454


2) Get date/time in seconds in Python

import time
import datetime

print time.time()
Output: Output will be something like this:

1506602961.08


3) Get formatted date/time in Python

import time
import datetime

print datetime.datetime.now().strftime("%y-%m-%d-%H-%M")
Output: Output will be something like this:

17-09-28-18-22


4) Get Current year and month in Python

import time
import datetime

print datetime.date.today().strftime("%Y")
print datetime.date.today().strftime("%B")
Output: Output will be something like this:

2017
September


5) Day of the week, dat of month and day of year in python

import time
import datetime

print "Day of year: ", datetime.date.today().strftime("%j")
print "Day of the month : ", datetime.date.today().strftime("%d")
print "Day of week: ", datetime.date.today().strftime("%A")
Output: Output will be something like this:

Day of year: 271
Day of the month : 28
Day of week: Thursday


In this article we have seen, Python Calendar, Date & Time with examples. In upcoming articles we will see more complex usecases and tricks of python.
  • By Techburps.com
  • Sep 30, 2017
  • Python