Python library defines a function that can be primarily used to get current time and date. now() function Return the current local date and time, which is defined under
Syntax : datetime.now(tz)
tz : Specified time zone of which current time and date is required. (Uses Greenwich Meridian time by default.)
Returns : Returns the current date and time in time format.
Code #1 :
Time now at greenwich meridian is : 2018-03-29 10:26:23.473031
Attributes of now() :
now() has different attributes, same as attributes of time such as year, month, date, hour, minute, second.
Code #2 : Demonstrate attributes of now().
The attributes of now() are : Year : 2018 Month : 3 Day : 26 Hour : 20 Minute : 9 Second : 4 Microsecond : 499806
Getting time of particular timezone :
Sometimes, need is just to get the current time of particular timezone. now() takes timezone as input to give timezone oriented output time. But these time zones are defined in pytz library.
Code #3 : Use of now() to handle specific timezone.
The current time in india is : 2018-03-29 03:09:33.878000+05:30
Note : Above code won’t work on online IDE due to absence of pytz module.
While developing any real world application, we might need to show real time of any timezone. now() function can do the work here very efficiently and easily.
- Python | oct() function
- Python | hex() function
- Python | cmp() function
- Python | int() function
- sum() function in Python
- ord() function in Python
- id() function in Python
- Python | dir() function
- Python map() function
- Python | How to get function name ?
- Help function in Python
- vars() function in Python
- Python | bytearray() function
- Python | randint() function
- Sum 2D array in Python using map() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.