In python getting a time and date can do by import one of them modules DateTime and time. Python DateTime is more preferable to get date and time. Both time and DateTime modules are inbuilt, you don’t need any 3rd party library.
In this tutorial, you will learn how to get the current time, today’s date, and python DateTime timezone overview with an example.
Example of Python DateTime now
The example of getting the current date and time in python using the DateTime module and class.
import datetime print(datetime.datetime.now())
Output: 2018-10-12 16:04:42.036760
The output is in the format of YYYY-MM-DD HH:MM:SS:MS
To complete detailed example must read: Python Date Function
Python Time now Example
import time # Time in second print("Time : ", time.time()) # Time with associated attribute and values print(time.localtime(time.time()))
Output: Time : 1539340637.700062
time.struct_time(tm_year=2018, tm_mon=10, tm_mday=12, tm_hour=16, tm_min=7, tm_sec=17, tm_wday=4, tm_yday=285, tm_isdst=0)
Here is seperate tutorial on time function – Python Time Function
How to get a Specific string from the DateTime object
Using strftime () Method you can get a specific string from a datetime object, for an example year, day, month etc.
here is an example of getting the full name of weekday from today date
import datetime # getting Full name of Weekday print(datetime.datetime.now().strftime("%A"))
Output: Friday
Format codes in strftime:
Here is import format code for strftime.
Directive | Description | Example |
---|---|---|
%a | Short name of weekday | Fri |
%A | Full name of Weekday | Friday |
%w | Weekday as a number 0-6, 0 is Sunday – 0 Monday – 1 … | 5 |
%d | Day of month 01-31 | 01 |
%b | Month name, short version | Oct |
%B | Month name, full version | October |
%m | Month as a number 01-12 | 10 |
%y | Year, short version, without century | 18 |
%Y | Year, full version | 2018 |
%H | Hour 00-23 | 00 |
%I | Hour 00-12 | 12 |
%p | AM/PM | AM |
%M | Minute 00-59 | 38 |
%S | Second 00-59 | 01 |
%f | Microsecond 000000-999999 | 844628 |
%z | UTC offset | +0100 |
%Z | Timezone | UTC+01:00 |
%j | Day number of year 001-366 | 283 |
%U | Week number of year, Sunday as the first day of the week, 00-53 | 40 |
%W | Week number of year, Monday as the first day of the week, 00-53 | 41 |
%c | Local version of date and time | Wed Oct 10 03:38:01 2018 |
%x | Local version of date | 10/10/18 |
%X | Local version of time | 03:38:01 |
%% | A % character | % |
Note: Examples are based on datetime.datetime(2018, 10, 10, 3, 38, 1, 844628)
Tutorial for a strftime method: Python strftime Function
Python DateTime TimeZone
Getting current timezone will be none, you have to use the import module for a date or add timezone using tzinfo.
import datetime print(datetime.datetime.now().tzinfo) print(datetime.datetime(2018, 10, 10, 3, 38, 1, 844628, tzinfo=datetime.timezone(datetime.timedelta(0, 3600))).tzinfo)
Output: None
UTC+01:00
QA: Difference between two dates.
You can calculate a number of days between two dates using date function like this.
from datetime import date date0 = date(2018, 8, 18) date1 = date(2019, 9, 26) delta = date1 - date0 print(delta.days)
Output: 404
Do comment if you have any doubt and suggestion on this tutorial.
Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6Python 3.7
All examples are in Python 3, so it may change its different from python 2 or upgraded versions.