Prerequisite: datetime module
In Python, date and time are not a data type of its own, but a module named
datetime can be imported to work with the date as well as time. Datetime module comes built into Python, so there is no need to install it externally.
To get both current date and time
datetime.now() function of
datetime module is used. This function returns the current local date and time.
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.
Time now at greenwich meridian is : 2019-12-11 13:54:30.967356
Attributes of now() :
now() has different attributes, same as attributes of time such as year, month, date, hour, minute, second.
The attributes of now() are : Year : 2019 Month : 12 Day : 11 Hour : 13 Minute : 56 Second : 7 Microsecond : 292616
Getting time of particular timezone :
In the above example, it can be seen that above code does not gives the current date and time of your timezone. To get the date and time for a particular timezone
now() takes timezone as input to give timezone oriented output time. But these time zones are defined in pytz library.
The current time in india is : 2019-12-11 19:28:23.973616+05:30
- Get current date using Python
- Python program to find difference between current time and given time
- Python | Create an empty text file with current date as its name
- Get current time in milliseconds using Python
- Python program to get Current Time
- Get Current Time in different Timezone using Python
- Time Functions in Python | Set-2 (Date Manipulations)
- Python | Working with date and time using Pandas
- Display date and time in videos using OpenCV - Python
- Python Tweepy – Getting the date and time when a tweet was tweeted
- PyQt5 QDateEdit - Getting Date Time
- PyQt5 QDateEdit - Getting Minimum Date Time which can be set
- PyQt5 QDateEdit - Setting Date Time
- PyQt5 QDateEdit - Getting Maximum Date Time which can be set
- PyQt5 QDateTimeEdit – Selecting both Date and Time text
- PyQt5 QDateEdit - Removing Maximum Date Time which can be set
- PyQt5 QDateEdit - Setting Maximum Date Time which can be set
- PyQt5 QDateTimeEdit – Getting Special Date Time Text
- PyQt5 QDateEdit - Removing Minimum Date Time which can be set
- PyQt5 QDateEdit - Date Time Changed Signal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.