-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatetime.py
179 lines (124 loc) · 5.66 KB
/
Datetime.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Python Datetime
# Python Dates
# A date in Python is not a data type of its own, but we can import a module named datetime to work with dates as date objects.
# Example
# Import the datetime module and display the current date:
import datetime
x = datetime.datetime.now()
print(x)
# Date Output
# When we execute the code from the example above the result will be:
# 2022-07-10 23:14:49.281849
# The date contains year, month, day, hour, minute, second, and microsecond.
# The datetime module has many methods to return information about the date object.
# Here are a few examples, you will learn more about them later in this chapter:
# Example
# Return the year and name of weekday:
import datetime
x = datetime.datetime.now()
print(x.year)
print(x.strftime("%A"))
# Creating Date Objects
# To create a date, we can use the datetime() class (constructor) of the datetime module.
# The datetime() class requires three parameters to create a date: year, month, day.
# Example
# Create a date object:
import datetime
x = datetime.datetime(2020, 5, 17)
print(x)
# The datetime() class also takes parameters for time and timezone (hour, minute, second, microsecond, tzone), but they are optional, and has a default value of 0, (None for timezone).
# The strftime() Method
# The datetime object has a method for formatting date objects into readable strings.
# The method is called strftime(), and takes one parameter, format, to specify the format of the returned string:
# Example
# Display the name of the month:
import datetime
x = datetime.datetime(2018, 6, 1)
print(x.strftime("%B"))
# A reference of all the legal format codes:
import datetime
# Directive Description Example
# %a Weekday, short version Wed
x = datetime.datetime.now()
print(x.strftime("%a"))
# %A Weekday, full version Wednesday
x = datetime.datetime.now()
print(x.strftime("%A"))
# %w Weekday as a number 0-6, 0 is Sunday 3
x = datetime.datetime.now()
print(x.strftime("%w"))
# %d Day of month 01-31 31
x = datetime.datetime.now()
print(x.strftime("%d"))
# %b Month name, short version Dec
x = datetime.datetime.now()
print(x.strftime("%b"))
# %B Month name, full version December
x = datetime.datetime.now()
print(x.strftime("%B"))
# %m Month as a number 01-12 12
x = datetime.datetime.now()
print(x.strftime("%m"))
# %y Year, short version, without century 18
x = datetime.datetime.now()
print(x.strftime("%y"))
# %Y Year, full version 2018
x = datetime.datetime.now()
print(x.strftime("%Y"))
# %H Hour 00-23 17
x = datetime.datetime.now()
print(x.strftime("%H"))
# %I Hour 00-12 05
x = datetime.datetime.now()
print(x.strftime("%I"))
# %p AM/PM PM
x = datetime.datetime.now()
print(x.strftime("%p"))
# %M Minute 00-59 41
x = datetime.datetime.now()
print(x.strftime("%M"))
# %S Second 00-59 08
x = datetime.datetime.now()
print(x.strftime("%S"))
# %f Microsecond 000000-999999 548513
x = datetime.datetime.now()
print(x.strftime("%f"))
# %z UTC offset +0100
x = datetime.datetime.now()
print(x.strftime("%z"))
# %Z Timezone CST
x = datetime.datetime.now()
print(x.strftime("%Z"))
# %j Day number of year 001-366 365
x = datetime.datetime.now()
print(x.strftime("%j"))
# %U Week number of year, Sunday as the first day of week, 00-53 52
x = datetime.datetime.now()
print(x.strftime("%U"))
# %W Week number of year, Monday as the first day of week, 00-53 52
x = datetime.datetime.now()
print(x.strftime("%W"))
# %c Local version of date and time Mon Dec 31 17:41:00 2018
x = datetime.datetime.now()
print(x.strftime("%c"))
# %C Century 20
x = datetime.datetime.now()
print(x.strftime("%C"))
# %x Local version of date 12/31/18
x = datetime.datetime.now()
print(x.strftime("%x"))
# %X Local version of time 17:41:00
x = datetime.datetime.now()
print(x.strftime("%X"))
# *%% A % character %
x = datetime.datetime.now()
print(x.strftime("%%"))
# %G ISO 8601 year 2018
x = datetime.datetime.now()
print(x.strftime("%G"))
# %u ISO 8601 weekday (1-7) 1
x = datetime.datetime.now()
print(x.strftime("%u"))
# %V ISO 8601 week-number (01-53) 01
x = datetime.datetime.now()
print(x.strftime("%V"))