1from datetime import datetime
2now = datetime.now()
3print (now.strftime("%Y-%m-%d %H:%M:%S"))
4
5
6Output: 2020-06-19 10:34:45
1# Example usage:
2import datetime
3date_time = datetime.datetime.now()
4print(date_time)
5--> 2020-10-03 15:29:54.822751
6
7# From the date_time variable, you can extract the date in various
8# custom formats with .strftime(), for example:
9date_time.strftime("%d/%m/%Y")
10--> '03/10/2020' # dd/mm/yyyy
11
12date_time.strftime("%m/%d/%y")
13--> '10/03/20' # mm/dd/yy
14
15date_time.strftime("%Y/%m/%d")
16--> '2020/10/03'
17
18date_time.strftime("%Y-%m-%d")
19--> '2020-10-03'
20
21date_time.strftime("%B %d, %Y")
22--> 'October 03, 2020'
23
24# Key for other custom date/time formats:
25Directive Description Example
26%a Weekday, short version Wed
27%A Weekday, full version Wednesday
28%w Weekday as a number 0-6, 0 is Sunday 3
29%d Day of month 01-31 31
30%b Month name, short version Dec
31%B Month name, full version December
32%m Month as a number 01-12 12
33%y Year, short version, without century 18
34%Y Year, full version 2018
35%H Hour 00-23 17
36%I Hour 00-12 05
37%p AM/PM PM
38%M Minute 00-59 41
39%S Second 00-59 08
40%f Microsecond 000000-999999 548513
41%z UTC offset +0100
42%Z Timezone CST
43%j Day number of year 001-366 365
44%U Week number of year 00-53 52
45%c Local version of date and time Mon Dec 31 17:41:00 2018
46%x Local version of date 12/31/18
47%X Local version of time 17:41:00
48%% A % character %
1from datetime import datetime
2now = datetime.now()
3print("date and time now: ", now)
4
5#you can also personalize how the formats, example:
6dt = now.strftime("%d/%m/%Y %H:%M:%S")
7print("date and time now: ", dt)
8
9#the output will be
10#date and time now: 22/12/2020 01:19:32
1from datetime import datetime
2
3# datetime object containing current date and time
4now = datetime.now()
5
6print("now =", now)
7
8# dd/mm/YY H:M:S
9dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
10print("date and time =", dt_string)
11
12# now = 2021-06-25 07:58:56.550604
13# date and time = 25/06/2021 07:58:56
1date_and_time = datetime.now()
2print("The today current date and time is:- ",date_and_time)