-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.py
More file actions
65 lines (41 loc) · 1.19 KB
/
date.py
File metadata and controls
65 lines (41 loc) · 1.19 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
#!/usr/bin/env python3
import time
# this is how to format a date
date=time.strftime("%Y-%m-%d",time.localtime())
i=input('Please input a date: (ex %s)\n' % date)
while len(i) != 10:
i=input('The format is wrong, Please input a date: ex %s\n' % date)
j=list(i)
for p in range(len(i)):
if (p==4) or (p==7):
if j[p] != '-':
print ('The date format is wrong...')
exit (2)
else:
try:
j[p]=int(j[p])
except ValueError:
print('The date format is wrong...')
exit (2)
year=i[:4]
#this is how to transform a list of int to string
year=int("".join(str(i) for i in year))
month=j[5:7]
month=int("".join(str(i) for i in month))
day=j[8:10]
day=int("".join(str(i) for i in day))
if month >= 13:
print('%s cannot be a value for month.' % month)
exit (2)
if day > 31:
print ('%s cannot be a value for day in a month' % day)
exit (2)
months = (0,31,59,90,120,151,181,212,243,273,304,334)
sum=months[month-1]
sum+=day
leap=0
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
leap=1
if (leap==1) and (month>2):
sum+=1
print ('%s is the %s day in year %s' % (i,sum,year), end='')