回答編集履歴
2
バグ修正
test
CHANGED
@@ -18,31 +18,25 @@
|
|
18
18
|
|
19
19
|
|
20
20
|
|
21
|
+
# コード
|
22
|
+
|
23
|
+
|
24
|
+
|
21
25
|
```python
|
26
|
+
|
27
|
+
# coding: utf-8
|
28
|
+
|
29
|
+
# Your code here!
|
30
|
+
|
31
|
+
|
22
32
|
|
23
33
|
from datetime import datetime, timedelta
|
24
34
|
|
25
35
|
|
26
36
|
|
27
|
-
old_datetime = datetime(2020,
|
37
|
+
old_datetime = datetime(2020, 2, 5)
|
28
38
|
|
29
|
-
today_datetime = datetime.today()
|
39
|
+
today_datetime = datetime.today().replace(hour=0,minute=0,second=0,microsecond=0)
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
# 日付は一旦おいて年月を月換算にする
|
34
|
-
|
35
|
-
years_diff = today_datetime.year - old_datetime.year
|
36
|
-
|
37
|
-
month_diff = (today_datetime.month - old_datetime.month) + years_diff * 12
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
# 日付の扱い。定期券を同じ考え方で負数になる場合は、1カ月経過していないという事
|
42
|
-
|
43
|
-
if today_datetime.day - old_datetime.day < 0:
|
44
|
-
|
45
|
-
month_diff -= 1;
|
46
40
|
|
47
41
|
|
48
42
|
|
@@ -54,18 +48,104 @@
|
|
54
48
|
|
55
49
|
|
56
50
|
|
57
|
-
|
51
|
+
# 月の扱い
|
58
52
|
|
59
|
-
|
53
|
+
month_diff = (today_datetime.month - old_datetime.month) + (today_datetime.year - old_datetime.year) * 12
|
60
54
|
|
61
|
-
|
55
|
+
if today_datetime.day - old_datetime.day < 0:
|
62
56
|
|
57
|
+
month_diff -= 1
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
# 年の扱い
|
62
|
+
|
63
|
+
years_diff = month_diff // 12
|
64
|
+
|
65
|
+
|
66
|
+
|
63
|
-
print(f'{o
|
67
|
+
print(f'{today_datetime} と {old_datetime} の比較')
|
68
|
+
|
69
|
+
print(f'{years_diff} 年前、{month_diff} カ月前、{week_diff} 週間前、{date_diff} 日前')
|
64
70
|
|
65
71
|
```
|
66
72
|
|
67
73
|
|
68
74
|
|
69
|
-
動作サンプル
|
75
|
+
# 動作サンプル
|
70
76
|
|
71
77
|
[https://paiza.io/projects/LW3wmLYdNQoPKsB3sgc9FQ?language=python3](https://paiza.io/projects/LW3wmLYdNQoPKsB3sgc9FQ?language=python3)
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
# 追記した悪乗り版コード
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
```
|
88
|
+
|
89
|
+
from datetime import datetime, timedelta
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
def test(old_datetime):
|
94
|
+
|
95
|
+
today_datetime = datetime.today().replace(hour=0,minute=0,second=0,microsecond=0)
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
# 日数と週の扱い
|
100
|
+
|
101
|
+
date_diff = (today_datetime - old_datetime).days
|
102
|
+
|
103
|
+
week_diff = date_diff // 7
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
# 月の扱い
|
108
|
+
|
109
|
+
month_diff = (today_datetime.month - old_datetime.month) + (today_datetime.year - old_datetime.year) * 12
|
110
|
+
|
111
|
+
if today_datetime.day - old_datetime.day < 0:
|
112
|
+
|
113
|
+
month_diff -= 1
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
# 年の扱い
|
118
|
+
|
119
|
+
years_diff = month_diff // 12
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
print(f'{old_datetime} は、{years_diff} 年前、{month_diff} カ月前、{week_diff} 週間前、{date_diff} 日前')
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
print(f'今日は {datetime.today()} です。')
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
test(datetime(2021, 2, 4))
|
134
|
+
|
135
|
+
test(datetime(2021, 2, 3))
|
136
|
+
|
137
|
+
test(datetime(2021, 2, 1))
|
138
|
+
|
139
|
+
test(datetime(2021, 1, 31))
|
140
|
+
|
141
|
+
test(datetime(2020, 12, 4))
|
142
|
+
|
143
|
+
test(datetime(2020, 2, 6))
|
144
|
+
|
145
|
+
test(datetime(2020, 2, 5))
|
146
|
+
|
147
|
+
test(datetime(2020, 2, 4))
|
148
|
+
|
149
|
+
```
|
150
|
+
|
151
|
+
[https://paiza.io/projects/_YmaBQlwVecdO9JDagJKvA](https://paiza.io/projects/_YmaBQlwVecdO9JDagJKvA)
|
1
週と日数も追加
test
CHANGED
@@ -24,7 +24,7 @@
|
|
24
24
|
|
25
25
|
|
26
26
|
|
27
|
-
old_datetime = datetime(202
|
27
|
+
old_datetime = datetime(2020, 7, 7)
|
28
28
|
|
29
29
|
today_datetime = datetime.today()
|
30
30
|
|
@@ -46,9 +46,21 @@
|
|
46
46
|
|
47
47
|
|
48
48
|
|
49
|
+
# 日数と週の扱い
|
50
|
+
|
49
|
-
|
51
|
+
date_diff = (today_datetime - old_datetime).days
|
52
|
+
|
53
|
+
week_diff = date_diff // 7
|
50
54
|
|
51
55
|
|
56
|
+
|
57
|
+
print(f'{old_datetime} は {years_diff} 年前')
|
58
|
+
|
59
|
+
print(f'{old_datetime} は {month_diff} カ月前')
|
60
|
+
|
61
|
+
print(f'{old_datetime} は {week_diff} 週間前')
|
62
|
+
|
63
|
+
print(f'{old_datetime} は {date_diff} 日前')
|
52
64
|
|
53
65
|
```
|
54
66
|
|