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
1from datetime import date
2
3today = date.today()
4print("Today's date:", today)
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 date
2//today is an object so you can get the day, month and year by
3//treating it as an object. eg: today.day, today.month, today.year
4today = date.today()
5print("Today's date:", today)