How to get current time and date in python

Hello Friends,

Python Logo

These days I am learning python. Recently while doing some work I have to get current time and date using python. In python there is datetime module for doing this. I am giving a simple example for that.

Python Code
import datetime
now = datetime.datetime.now()
print
print "Current date and time using str method of datetime object:"
print str(now)
print
print "Current date and time using instance attributes:"
print "Current year: %d" % now.year
print "Current month: %d" % now.month
print "Current day: %d" % now.day
print "Current hour: %d" % now.hour
print "Current minute: %d" % now.minute
print "Current second: %d" % now.second
print "Current microsecond: %d" % now.microsecond
print
print "Current date and time using strftime:"
print now.strftime("%Y-%m-%d %H:%M")

And the result will be:

Current date and time using str method of datetime object:
2008-06-26 11:33:15.309236

Current date and time using instance attributes:
Current year: 2008
Current month: 6
Current day: 26
Current hour: 11
Current minute: 33
Current second: 15
Current microsecond: 309236

Current date and time using strftime:
2008-06-26 11:33

Directly from the time module documentation, here are more options to use with strftime:

Directives Meaning
%a Locale’s abbreviated weekday name.
%A Locale’s full weekday name.
%b Locale’s abbreviated month name.
%B Locale’s full month name.
%c Locale’s appropriate date and time representation.
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%I Hour (12-hour clock) as a decimal number [01,12].
%j Day of the year as a decimal number [001,366].
%m Month as a decimal number [01,12].
%M Minute as a decimal number [00,59].
%p Locale’s equivalent of either AM or PM.
%S Second as a decimal number [00,61].
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.
%w Weekday as a decimal number [0(Sunday),6].
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
%x Locale’s appropriate date representation.
%X Locale’s appropriate time representation.
%y Year without century as a decimal number [00,99].
%Y Year with century as a decimal number.
%Z Time zone name (no characters if no time zone exists).
%% A literal “%” character.

References:

[1] http://docs.python.org/library/datetime.html

[2] http://tinyurl.com/22taqtl

Advertisements

About bijolianabhi

Graduate from Engineering College Bikaner, India in 2010 and working as Open Source activist. I love programming and implementing interesting ideas. I have interest in Mozilla Firefox extension development. I am currently learning basics of python to develop some interesting applications.
This entry was posted in Programming, Python, Tutorials and tagged , , , , , , , , , , , . Bookmark the permalink.

4 Responses to How to get current time and date in python

  1. Brenton Sperow says:

    Damn it is working..!! Thanks..

  2. GGG Bukkake says:

    Thanks , I have just been looking for information about this topic for a long time and yours is the best I’ve discovered so far. But, what about the bottom line? Are you sure in regards to the source?|What i don’t understood is in truth how you’re no longer really a lot more neatly-favored than you may be now. You are so intelligent.

  3. tabata says:

    Do you have a Facebook fan page for your site?

  4. Polek Wignall says:

    Excellent submit, very informative. I ponder why the opposite experts of this sector don’t notice this. You must continue your writing. I am confident, you have a huge readers’ base already!|What’s Taking place i’m new to this, I stumbled upon this I’ve discovered It positively useful and it has helped me out loads. I am hoping to contribute & assist other users like its aided me. Good job.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s