-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTimeConversion.py
More file actions
164 lines (125 loc) · 4.46 KB
/
DateTimeConversion.py
File metadata and controls
164 lines (125 loc) · 4.46 KB
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
import datetime
import time
'''
datetime is a module in python, that provides us with 6 classes, which has multiple useful functions.
We dont need to install this module seperately, it comes with the default installation of python.
Classes given by datetime module are given below:
1- date
2- time
3- datetime
4- timedelta
5- tzinfo
6- timezone
every class has many functions and we will explore all of them InshAllah
'''
# Date class
def getDate():
date = datetime.date(1997,10,10)
print("date is: ", date)
print("Type of date is: ",type(date))
# Uncommenting my_date = date(1996, 12, 39)
# will raise an ValueError as it is
# outside range
# uncommenting my_date = date('1996', 12, 11)
# will raise a TypeError as a string is
# passed instead of integer
def getTime():
time = datetime.time(hour = 11,minute = 34, second = 56, microsecond=11)
print("Hours: " , time.hour)
print("Minutes: ", time.minute)
print("Seconds: ", time.second)
print("Microseconds: ", time.microsecond)
def convertingTimeObjectIntoString():
time = datetime.time(hour = 11,minute = 34, second = 56, microsecond=11)
print("Hours: " , time.hour)
print("Minutes: ", time.minute)
print("Seconds: ", time.second)
print("Microseconds: ", time.microsecond)
print(type(time.hour))
print(type(time))
timeInStr = time.isoformat()
# Seconds will be caluclated upto 6 decimal digits (Microseonds)
print("Time in String object is: ", timeInStr, " Type of this time is: ", type(timeInStr))
def convetingStringTimeObjectintoTimeClassObject():
time = datetime.time(hour = 11,minute = 34, second = 56, microsecond=11)
print("Hours: " , time.hour)
print("Minutes: ", time.minute)
print("Seconds: ", time.second)
print("Microseconds: ", time.microsecond)
print(type(time.hour))
print(type(time))
timeInStr = time.isoformat()
# Seconds will be caluclated upto 6 decimal digits (Microseonds)
print("Time in String object is: ", timeInStr, " Type of this time is: ", type(timeInStr))
timeObjectConversion = datetime.time.fromisoformat(timeInStr)
print(type(timeObjectConversion))
# getDate()
# getTime()
def replaceFunc():
time = datetime.time(hour = 11)
replacement = time.replace(hour=5)
print(replacement)
def strfTimeConversion():
time = datetime.time(hour= 11, minute = 34)
replacement = time.strftime("%H/%M")
print(replacement)
def datetimeFunc():
a = datetime.datetime(1999, 12, 12)
print(a)
# Initializing constructor
# with time parameters as well
a = datetime.datetime(1999, 12, 12, 12, 12, 12, 342380)
print(a)
def getDateTimeChunks():
a = datetime.datetime(1999, 1, 12, 12, 12, 12)
print("year =", a.year)
print("month =", a.month)
print("hour =", a.hour)
print("minute =", a.minute)
print("timestamp =", datetime.datetime.timestamp(a))
def currentDateTime():
currentDateAndTime = datetime.datetime.now()
print(currentDateAndTime)
def getTodaysDate():
todaysDate = datetime.date.today()
print(todaysDate)
def getTodaysDateandSplitIntoChunks():
todaysDate = datetime.date.today()
print(todaysDate.year)
print(todaysDate.month)
print(todaysDate.day)
def convertingDateIntoString():
date = datetime.date.today()
# dateIntoString = date.isoformat()
# OR we can write below line too
dateIntoString = datetime.date.isoformat(date)
print(dateIntoString)
print(type(dateIntoString))
def astimeZone():
d1 = datetime.datetime.now()
# Calling the astimezone() function without
# any timezone parameter
d2 = d1.astimezone()
# Printing the local current date, time and
# timezone
print(format(d2))
def dateTimeCombineFunction():
d = datetime.date.today()
t = datetime.datetime.now().time()
print(datetime.datetime.combine(d,t))
print(d,t)
def ctimeOfDateTime():
Todays_time = time.time()
# Printing today's time
print(Todays_time)
# Calling the fromtimestamp() function
# to get date from the current time
date_From_CurrentTime = datetime.date.fromtimestamp(Todays_time);
# Printing the current date
print("Date for Timestamp used is: %s"%date_From_CurrentTime);
# Calling the ctime() function over the above date
print("Today's date: %s"%date_From_CurrentTime.ctime());
def convertingDateIntoString():
date = datetime.date.today()
print(datetime.date.isoformat(date))
convertingDateIntoString()