回答編集履歴

3

修正

2019/10/22 06:40

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -68,14 +68,22 @@
68
68
 
69
69
 
70
70
 
71
- ### ****
71
+ ### **無理にでもstrftimeを使う方法**
72
+
73
+ 思い付きです。避けた方が良いでしょう。
72
74
 
73
75
  ```Python
74
76
 
75
77
  print(
76
78
 
77
- (datetime.datetime(2000, 1, 1) + p3).strftime('%H:%M')
79
+ (datetime.datetime(2000, 1, 1) + p3).strftime('%H:%M') # ただし、形式は 03:00
78
80
 
79
81
  )
80
82
 
81
83
  ```
84
+
85
+
86
+
87
+ どうしても 3:00 と出力したい場合、Linuxであればフォーマット %-I:%M が使えるそうです。
88
+
89
+ [Python datetime formatting without zero-padding - Stack Overflow](https://stackoverflow.com/questions/9525944/python-datetime-formatting-without-zero-padding)

2

追記

2019/10/22 06:40

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -65,3 +65,17 @@
65
65
  **参考**
66
66
 
67
67
  - [組み込み型 - str — Python 3.8.0 ドキュメント](https://docs.python.org/ja/3/library/stdtypes.html#text-sequence-type-str)
68
+
69
+
70
+
71
+ ### ****
72
+
73
+ ```Python
74
+
75
+ print(
76
+
77
+ (datetime.datetime(2000, 1, 1) + p3).strftime('%H:%M')
78
+
79
+ )
80
+
81
+ ```

1

追記

2019/10/22 06:37

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -8,7 +8,13 @@
8
8
 
9
9
 
10
10
 
11
- **必要な情報を計算する方法**
11
+ ### **必要な情報を計算する方法**
12
+
13
+ timedeltaオブジェクトは days, seconds, microseconds しか提供していません。
14
+
15
+ 面倒ですが時分は自力で計算します。
16
+
17
+
12
18
 
13
19
  ```Python
14
20
 
@@ -26,9 +32,17 @@
26
32
 
27
33
 
28
34
 
35
+ **参考**
36
+
37
+ - [datetime - timedelta — Python 3.8.0 ドキュメント](https://docs.python.org/ja/3/library/datetime.html#timedelta-objects)
38
+
39
+ - [組み込み関数- divmod — Python 3.8.0 ドキュメント](https://docs.python.org/ja/3/library/functions.html#divmod)
29
40
 
30
41
 
42
+
31
- **不要な情報を削ぎ落す方法**
43
+ ### **不要な情報を削ぎ落す方法**
44
+
45
+ 邪道な気がしますが、楽ではあります。
32
46
 
33
47
  ```Python
34
48
 
@@ -38,4 +52,16 @@
38
52
 
39
53
 
40
54
 
55
+ あるいは
56
+
41
- 後者は邪道な感がしますが、楽ではあります。
57
+ ```Python
58
+
59
+ print(str(p3).rstrip('0123456789')[:-1])
60
+
61
+ ```
62
+
63
+
64
+
65
+ **参考**
66
+
67
+ - [組み込み型 - str — Python 3.8.0 ドキュメント](https://docs.python.org/ja/3/library/stdtypes.html#text-sequence-type-str)