-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetime_modules.py
More file actions
55 lines (45 loc) · 1.41 KB
/
datetime_modules.py
File metadata and controls
55 lines (45 loc) · 1.41 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
import datetime
#print current datetime
cur_datetime=datetime.datetime.now()
print("Current datetime:",cur_datetime)
#convert string int datetime object
str="Feb 25 2020 4:20PM"
str_1=datetime.datetime.strptime(str,"%b %d %Y %I:%M%p")
print(str_1)
#subtract a week from given date
given_date=datetime.datetime(2023,2,25)
time_delta=datetime.timedelta(days=7)
print("New_date:",given_date-time_delta)
#print date following format: Tuesday 25 February 2020
give_date=datetime.datetime(2023,2,25)
res_date=give_date.strftime("%A %d %B %Y")
print(res_date)
#find the day of week of given date
give=datetime.datetime(2020,7,26)
print(give.strftime("%A"))
#Add a week(7days) and 12 hours to given date
user=datetime.datetime(2023,3,22,10,0,0)
time_delta1=datetime.timedelta(days=7,hours=12)
res=user+time_delta1
print(res.strftime("%Y-%m-%d %I:%M:%S"))
#print current time in milliseconds
import time
milli=int(round(time.time()*1000))
print(milli)
#Convert datetime into string
g=datetime.datetime(2023,2,25)
f=g.strftime("%Y-%m-%d %H:%M:%S")
print(f)
#calculate the date 4 months from current date
c=datetime.datetime.now()
td=datetime.timedelta(4*365/12)
r=c+td
print(r.strftime("%Y-%m-%d"))
#Calculate number of days between two given date
g1=datetime.datetime(2023,2,25).date()
g2=datetime.datetime(2023,9,17).date()
if g1>g2:
delta=g1-g2
else:
delta=g2-g1
print(delta.days)