-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetime.html
More file actions
2789 lines (2630 loc) · 308 KB
/
datetime.html
File metadata and controls
2789 lines (2630 loc) · 308 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_TW">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>8.1. datetime — Basic date and time types — Python 3.7.0 說明文件</title>
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/translations.js"></script>
<script type="text/javascript" src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.7.0 說明文件 中搜尋"
href="../_static/opensearch.xml"/>
<link rel="author" title="關於這些文件" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜尋" href="../search.html" />
<link rel="copyright" title="Copyright" href="../copyright.html" />
<link rel="next" title="8.2. calendar — General calendar-related functions" href="calendar.html" />
<link rel="prev" title="8. Data Types" href="datatypes.html" />
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
<link rel="canonical" href="https://docs.python.org/3/library/datetime.html" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/switchers.js"></script>
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>瀏覽</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模組索引"
>模組</a> |</li>
<li class="right" >
<a href="calendar.html" title="8.2. calendar — General calendar-related functions"
accesskey="N">下一頁</a> |</li>
<li class="right" >
<a href="datatypes.html" title="8. Data Types"
accesskey="P">上一頁</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<span class="language_switcher_placeholder">zh_TW</span>
<span class="version_switcher_placeholder">3.7.0</span>
<a href="../index.html">Documentation </a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 標準函式庫 (Standard Library)</a> »</li>
<li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">8. Data Types</a> »</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="module-datetime">
<span id="datetime-basic-date-and-time-types"></span><h1>8.1. <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> — Basic date and time types<a class="headerlink" href="#module-datetime" title="本標題的永久連結">¶</a></h1>
<p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/datetime.py">Lib/datetime.py</a></p>
<hr class="docutils" />
<p>The <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> module supplies classes for manipulating dates and times in
both simple and complex ways. While date and time arithmetic is supported, the
focus of the implementation is on efficient attribute extraction for output
formatting and manipulation. For related functionality, see also the
<a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</span></code></a> and <a class="reference internal" href="calendar.html#module-calendar" title="calendar: Functions for working with calendars, including some emulation of the Unix cal program."><code class="xref py py-mod docutils literal notranslate"><span class="pre">calendar</span></code></a> modules.</p>
<p>There are two kinds of date and time objects: 「naive」 and 「aware」.</p>
<p>An aware object has sufficient knowledge of applicable algorithmic and
political time adjustments, such as time zone and daylight saving time
information, to locate itself relative to other aware objects. An aware object
is used to represent a specific moment in time that is not open to
interpretation <a class="footnote-reference" href="#id2" id="id1">[1]</a>.</p>
<p>A naive object does not contain enough information to unambiguously locate
itself relative to other date/time objects. Whether a naive object represents
Coordinated Universal Time (UTC), local time, or time in some other timezone is
purely up to the program, just like it is up to the program whether a
particular number represents metres, miles, or mass. Naive objects are easy to
understand and to work with, at the cost of ignoring some aspects of reality.</p>
<p>For applications requiring aware objects, <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> and <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a>
objects have an optional time zone information attribute, <code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code>, that
can be set to an instance of a subclass of the abstract <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> class.
These <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> objects capture information about the offset from UTC
time, the time zone name, and whether Daylight Saving Time is in effect. Note
that only one concrete <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> class, the <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> class, is
supplied by the <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> module. The <a class="reference internal" href="#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> class can
represent simple timezones with fixed offset from UTC, such as UTC itself or
North American EST and EDT timezones. Supporting timezones at deeper levels of
detail is up to the application. The rules for time adjustment across the
world are more political than rational, change frequently, and there is no
standard suitable for every application aside from UTC.</p>
<p>The <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> module exports the following constants:</p>
<dl class="data">
<dt id="datetime.MINYEAR">
<code class="descclassname">datetime.</code><code class="descname">MINYEAR</code><a class="headerlink" href="#datetime.MINYEAR" title="本定義的永久連結">¶</a></dt>
<dd><p>The smallest year number allowed in a <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> or <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object.
<a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MINYEAR</span></code></a> is <code class="docutils literal notranslate"><span class="pre">1</span></code>.</p>
</dd></dl>
<dl class="data">
<dt id="datetime.MAXYEAR">
<code class="descclassname">datetime.</code><code class="descname">MAXYEAR</code><a class="headerlink" href="#datetime.MAXYEAR" title="本定義的永久連結">¶</a></dt>
<dd><p>The largest year number allowed in a <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> or <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object.
<a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAXYEAR</span></code></a> is <code class="docutils literal notranslate"><span class="pre">9999</span></code>.</p>
</dd></dl>
<div class="admonition seealso">
<p class="first admonition-title">也參考</p>
<dl class="last docutils">
<dt>Module <a class="reference internal" href="calendar.html#module-calendar" title="calendar: Functions for working with calendars, including some emulation of the Unix cal program."><code class="xref py py-mod docutils literal notranslate"><span class="pre">calendar</span></code></a></dt>
<dd>General calendar related functions.</dd>
<dt>Module <a class="reference internal" href="time.html#module-time" title="time: Time access and conversions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</span></code></a></dt>
<dd>Time access and conversions.</dd>
</dl>
</div>
<div class="section" id="available-types">
<h2>8.1.1. Available Types<a class="headerlink" href="#available-types" title="本標題的永久連結">¶</a></h2>
<dl class="class">
<dt>
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">date</code></dt>
<dd><p>An idealized naive date, assuming the current Gregorian calendar always was, and
always will be, in effect. Attributes: <a class="reference internal" href="#datetime.date.year" title="datetime.date.year"><code class="xref py py-attr docutils literal notranslate"><span class="pre">year</span></code></a>, <a class="reference internal" href="#datetime.date.month" title="datetime.date.month"><code class="xref py py-attr docutils literal notranslate"><span class="pre">month</span></code></a>, and
<a class="reference internal" href="#datetime.date.day" title="datetime.date.day"><code class="xref py py-attr docutils literal notranslate"><span class="pre">day</span></code></a>.</p>
</dd></dl>
<dl class="class">
<dt>
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">time</code></dt>
<dd><p>An idealized time, independent of any particular day, assuming that every day
has exactly 24*60*60 seconds (there is no notion of 「leap seconds」 here).
Attributes: <a class="reference internal" href="#datetime.time.hour" title="datetime.time.hour"><code class="xref py py-attr docutils literal notranslate"><span class="pre">hour</span></code></a>, <a class="reference internal" href="#datetime.time.minute" title="datetime.time.minute"><code class="xref py py-attr docutils literal notranslate"><span class="pre">minute</span></code></a>, <a class="reference internal" href="#datetime.time.second" title="datetime.time.second"><code class="xref py py-attr docutils literal notranslate"><span class="pre">second</span></code></a>, <a class="reference internal" href="#datetime.time.microsecond" title="datetime.time.microsecond"><code class="xref py py-attr docutils literal notranslate"><span class="pre">microsecond</span></code></a>,
and <a class="reference internal" href="#datetime.time.tzinfo" title="datetime.time.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a>.</p>
</dd></dl>
<dl class="class">
<dt>
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">datetime</code></dt>
<dd><p>A combination of a date and a time. Attributes: <a class="reference internal" href="#datetime.datetime.year" title="datetime.datetime.year"><code class="xref py py-attr docutils literal notranslate"><span class="pre">year</span></code></a>, <a class="reference internal" href="#datetime.datetime.month" title="datetime.datetime.month"><code class="xref py py-attr docutils literal notranslate"><span class="pre">month</span></code></a>,
<a class="reference internal" href="#datetime.datetime.day" title="datetime.datetime.day"><code class="xref py py-attr docutils literal notranslate"><span class="pre">day</span></code></a>, <a class="reference internal" href="#datetime.datetime.hour" title="datetime.datetime.hour"><code class="xref py py-attr docutils literal notranslate"><span class="pre">hour</span></code></a>, <a class="reference internal" href="#datetime.datetime.minute" title="datetime.datetime.minute"><code class="xref py py-attr docutils literal notranslate"><span class="pre">minute</span></code></a>, <a class="reference internal" href="#datetime.datetime.second" title="datetime.datetime.second"><code class="xref py py-attr docutils literal notranslate"><span class="pre">second</span></code></a>, <a class="reference internal" href="#datetime.datetime.microsecond" title="datetime.datetime.microsecond"><code class="xref py py-attr docutils literal notranslate"><span class="pre">microsecond</span></code></a>,
and <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a>.</p>
</dd></dl>
<dl class="class">
<dt>
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">timedelta</code></dt>
<dd><p>A duration expressing the difference between two <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a>, <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a>,
or <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> instances to microsecond resolution.</p>
</dd></dl>
<dl class="class">
<dt>
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">tzinfo</code></dt>
<dd><p>An abstract base class for time zone information objects. These are used by the
<a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> and <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> classes to provide a customizable notion of
time adjustment (for example, to account for time zone and/or daylight saving
time).</p>
</dd></dl>
<dl class="class">
<dt>
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">timezone</code></dt>
<dd><p>A class that implements the <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> abstract base class as a
fixed offset from the UTC.</p>
<div class="versionadded">
<p><span class="versionmodified">3.2 版新加入.</span></p>
</div>
</dd></dl>
<p>Objects of these types are immutable.</p>
<p>Objects of the <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> type are always naive.</p>
<p>An object of type <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> or <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> may be naive or aware.
A <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object <em>d</em> is aware if <code class="docutils literal notranslate"><span class="pre">d.tzinfo</span></code> is not <code class="docutils literal notranslate"><span class="pre">None</span></code> and
<code class="docutils literal notranslate"><span class="pre">d.tzinfo.utcoffset(d)</span></code> does not return <code class="docutils literal notranslate"><span class="pre">None</span></code>. If <code class="docutils literal notranslate"><span class="pre">d.tzinfo</span></code> is
<code class="docutils literal notranslate"><span class="pre">None</span></code>, or if <code class="docutils literal notranslate"><span class="pre">d.tzinfo</span></code> is not <code class="docutils literal notranslate"><span class="pre">None</span></code> but <code class="docutils literal notranslate"><span class="pre">d.tzinfo.utcoffset(d)</span></code>
returns <code class="docutils literal notranslate"><span class="pre">None</span></code>, <em>d</em> is naive. A <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> object <em>t</em> is aware
if <code class="docutils literal notranslate"><span class="pre">t.tzinfo</span></code> is not <code class="docutils literal notranslate"><span class="pre">None</span></code> and <code class="docutils literal notranslate"><span class="pre">t.tzinfo.utcoffset(None)</span></code> does not return
<code class="docutils literal notranslate"><span class="pre">None</span></code>. Otherwise, <em>t</em> is naive.</p>
<p>The distinction between naive and aware doesn’t apply to <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a>
objects.</p>
<p>Subclass relationships:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">object</span>
<span class="n">timedelta</span>
<span class="n">tzinfo</span>
<span class="n">timezone</span>
<span class="n">time</span>
<span class="n">date</span>
<span class="n">datetime</span>
</pre></div>
</div>
</div>
<div class="section" id="timedelta-objects">
<span id="datetime-timedelta"></span><h2>8.1.2. <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> Objects<a class="headerlink" href="#timedelta-objects" title="本標題的永久連結">¶</a></h2>
<p>A <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object represents a duration, the difference between two
dates or times.</p>
<dl class="class">
<dt id="datetime.timedelta">
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">timedelta</code><span class="sig-paren">(</span><em>days=0</em>, <em>seconds=0</em>, <em>microseconds=0</em>, <em>milliseconds=0</em>, <em>minutes=0</em>, <em>hours=0</em>, <em>weeks=0</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.timedelta" title="本定義的永久連結">¶</a></dt>
<dd><p>All arguments are optional and default to <code class="docutils literal notranslate"><span class="pre">0</span></code>. Arguments may be integers
or floats, and may be positive or negative.</p>
<p>Only <em>days</em>, <em>seconds</em> and <em>microseconds</em> are stored internally. Arguments are
converted to those units:</p>
<ul class="simple">
<li>A millisecond is converted to 1000 microseconds.</li>
<li>A minute is converted to 60 seconds.</li>
<li>An hour is converted to 3600 seconds.</li>
<li>A week is converted to 7 days.</li>
</ul>
<p>and days, seconds and microseconds are then normalized so that the
representation is unique, with</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">microseconds</span> <span class="pre"><</span> <span class="pre">1000000</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">seconds</span> <span class="pre"><</span> <span class="pre">3600*24</span></code> (the number of seconds in one day)</li>
<li><code class="docutils literal notranslate"><span class="pre">-999999999</span> <span class="pre"><=</span> <span class="pre">days</span> <span class="pre"><=</span> <span class="pre">999999999</span></code></li>
</ul>
<p>If any argument is a float and there are fractional microseconds,
the fractional microseconds left over from all arguments are
combined and their sum is rounded to the nearest microsecond using
round-half-to-even tiebreaker. If no argument is a float, the
conversion and normalization processes are exact (no information is
lost).</p>
<p>If the normalized value of days lies outside the indicated range,
<a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> is raised.</p>
<p>Note that normalization of negative values may be surprising at first. For
example,</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">timedelta</span>
<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">microseconds</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">>>> </span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">days</span><span class="p">,</span> <span class="n">d</span><span class="o">.</span><span class="n">seconds</span><span class="p">,</span> <span class="n">d</span><span class="o">.</span><span class="n">microseconds</span><span class="p">)</span>
<span class="go">(-1, 86399, 999999)</span>
</pre></div>
</div>
</dd></dl>
<p>Class attributes are:</p>
<dl class="attribute">
<dt id="datetime.timedelta.min">
<code class="descclassname">timedelta.</code><code class="descname">min</code><a class="headerlink" href="#datetime.timedelta.min" title="本定義的永久連結">¶</a></dt>
<dd><p>The most negative <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object, <code class="docutils literal notranslate"><span class="pre">timedelta(-999999999)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.timedelta.max">
<code class="descclassname">timedelta.</code><code class="descname">max</code><a class="headerlink" href="#datetime.timedelta.max" title="本定義的永久連結">¶</a></dt>
<dd><p>The most positive <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object, <code class="docutils literal notranslate"><span class="pre">timedelta(days=999999999,</span>
<span class="pre">hours=23,</span> <span class="pre">minutes=59,</span> <span class="pre">seconds=59,</span> <span class="pre">microseconds=999999)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.timedelta.resolution">
<code class="descclassname">timedelta.</code><code class="descname">resolution</code><a class="headerlink" href="#datetime.timedelta.resolution" title="本定義的永久連結">¶</a></dt>
<dd><p>The smallest possible difference between non-equal <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> objects,
<code class="docutils literal notranslate"><span class="pre">timedelta(microseconds=1)</span></code>.</p>
</dd></dl>
<p>Note that, because of normalization, <code class="docutils literal notranslate"><span class="pre">timedelta.max</span></code> > <code class="docutils literal notranslate"><span class="pre">-timedelta.min</span></code>.
<code class="docutils literal notranslate"><span class="pre">-timedelta.max</span></code> is not representable as a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object.</p>
<p>Instance attributes (read-only):</p>
<table border="1" class="docutils">
<colgroup>
<col width="29%" />
<col width="71%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Attribute</th>
<th class="head">Value</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">days</span></code></td>
<td>Between -999999999 and 999999999 inclusive</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">seconds</span></code></td>
<td>Between 0 and 86399 inclusive</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">microseconds</span></code></td>
<td>Between 0 and 999999 inclusive</td>
</tr>
</tbody>
</table>
<p>Supported operations:</p>
<table border="1" class="docutils">
<colgroup>
<col width="41%" />
<col width="59%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Operation</th>
<th class="head">Result</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">+</span> <span class="pre">t3</span></code></td>
<td>Sum of <em>t2</em> and <em>t3</em>. Afterwards <em>t1</em>-<em>t2</em> ==
<em>t3</em> and <em>t1</em>-<em>t3</em> == <em>t2</em> are true. (1)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">-</span> <span class="pre">t3</span></code></td>
<td>Difference of <em>t2</em> and <em>t3</em>. Afterwards <em>t1</em>
== <em>t2</em> - <em>t3</em> and <em>t2</em> == <em>t1</em> + <em>t3</em> are
true. (1)(6)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">*</span> <span class="pre">i</span> <span class="pre">or</span> <span class="pre">t1</span> <span class="pre">=</span> <span class="pre">i</span> <span class="pre">*</span> <span class="pre">t2</span></code></td>
<td>Delta multiplied by an integer.
Afterwards <em>t1</em> // i == <em>t2</em> is true,
provided <code class="docutils literal notranslate"><span class="pre">i</span> <span class="pre">!=</span> <span class="pre">0</span></code>.</td>
</tr>
<tr class="row-odd"><td> </td>
<td>In general, <em>t1</em> * i == <em>t1</em> * (i-1) + <em>t1</em>
is true. (1)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">*</span> <span class="pre">f</span> <span class="pre">or</span> <span class="pre">t1</span> <span class="pre">=</span> <span class="pre">f</span> <span class="pre">*</span> <span class="pre">t2</span></code></td>
<td>Delta multiplied by a float. The result is
rounded to the nearest multiple of
timedelta.resolution using round-half-to-even.</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">f</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">/</span> <span class="pre">t3</span></code></td>
<td>Division (3) of <em>t2</em> by <em>t3</em>. Returns a
<a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> object.</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">/</span> <span class="pre">f</span> <span class="pre">or</span> <span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">/</span> <span class="pre">i</span></code></td>
<td>Delta divided by a float or an int. The result
is rounded to the nearest multiple of
timedelta.resolution using round-half-to-even.</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">//</span> <span class="pre">i</span></code> or
<code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">//</span> <span class="pre">t3</span></code></td>
<td>The floor is computed and the remainder (if
any) is thrown away. In the second case, an
integer is returned. (3)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">t1</span> <span class="pre">=</span> <span class="pre">t2</span> <span class="pre">%</span> <span class="pre">t3</span></code></td>
<td>The remainder is computed as a
<a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object. (3)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">q,</span> <span class="pre">r</span> <span class="pre">=</span> <span class="pre">divmod(t1,</span> <span class="pre">t2)</span></code></td>
<td>Computes the quotient and the remainder:
<code class="docutils literal notranslate"><span class="pre">q</span> <span class="pre">=</span> <span class="pre">t1</span> <span class="pre">//</span> <span class="pre">t2</span></code> (3) and <code class="docutils literal notranslate"><span class="pre">r</span> <span class="pre">=</span> <span class="pre">t1</span> <span class="pre">%</span> <span class="pre">t2</span></code>.
q is an integer and r is a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a>
object.</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">+t1</span></code></td>
<td>Returns a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object with the
same value. (2)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">-t1</span></code></td>
<td>equivalent to <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a>(-<em>t1.days</em>, -<em>t1.seconds</em>,
-<em>t1.microseconds</em>), and to <em>t1</em>* -1. (1)(4)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">abs(t)</span></code></td>
<td>equivalent to +<em>t</em> when <code class="docutils literal notranslate"><span class="pre">t.days</span> <span class="pre">>=</span> <span class="pre">0</span></code>, and
to -<em>t</em> when <code class="docutils literal notranslate"><span class="pre">t.days</span> <span class="pre"><</span> <span class="pre">0</span></code>. (2)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">str(t)</span></code></td>
<td>Returns a string in the form
<code class="docutils literal notranslate"><span class="pre">[D</span> <span class="pre">day[s],</span> <span class="pre">][H]H:MM:SS[.UUUUUU]</span></code>, where D
is negative for negative <code class="docutils literal notranslate"><span class="pre">t</span></code>. (5)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">repr(t)</span></code></td>
<td>Returns a string representation of the
<a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object as a constructor
call with canonical attribute values.</td>
</tr>
</tbody>
</table>
<p>註解:</p>
<ol class="arabic">
<li><p class="first">This is exact, but may overflow.</p>
</li>
<li><p class="first">This is exact, and cannot overflow.</p>
</li>
<li><p class="first">Division by 0 raises <a class="reference internal" href="exceptions.html#ZeroDivisionError" title="ZeroDivisionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZeroDivisionError</span></code></a>.</p>
</li>
<li><p class="first">-<em>timedelta.max</em> is not representable as a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object.</p>
</li>
<li><p class="first">String representations of <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> objects are normalized
similarly to their internal representation. This leads to somewhat
unusual results for negative timedeltas. For example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=-</span><span class="mi">5</span><span class="p">)</span>
<span class="go">datetime.timedelta(days=-1, seconds=68400)</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">_</span><span class="p">)</span>
<span class="go">-1 day, 19:00:00</span>
</pre></div>
</div>
</li>
<li><p class="first">The expression <code class="docutils literal notranslate"><span class="pre">t2</span> <span class="pre">-</span> <span class="pre">t3</span></code> will always be equal to the expression <code class="docutils literal notranslate"><span class="pre">t2</span> <span class="pre">+</span> <span class="pre">(-t3)</span></code> except
when t3 is equal to <code class="docutils literal notranslate"><span class="pre">timedelta.max</span></code>; in that case the former will produce a result
while the latter will overflow.</p>
</li>
</ol>
<p>In addition to the operations listed above <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> objects support
certain additions and subtractions with <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> and <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>
objects (see below).</p>
<div class="versionchanged">
<p><span class="versionmodified">3.2 版更變: </span>Floor division and true division of a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object by another
<a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object are now supported, as are remainder operations and
the <a class="reference internal" href="functions.html#divmod" title="divmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">divmod()</span></code></a> function. True division and multiplication of a
<a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object by a <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> object are now supported.</p>
</div>
<p>Comparisons of <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> objects are supported with the
<a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object representing the smaller duration considered to be the
smaller timedelta. In order to stop mixed-type comparisons from falling back to
the default comparison by object address, when a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object is
compared to an object of a different type, <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised unless the
comparison is <code class="docutils literal notranslate"><span class="pre">==</span></code> or <code class="docutils literal notranslate"><span class="pre">!=</span></code>. The latter cases return <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a> or
<a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>, respectively.</p>
<p><a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> objects are <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a> (usable as dictionary keys), support
efficient pickling, and in Boolean contexts, a <a class="reference internal" href="#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> object is
considered to be true if and only if it isn’t equal to <code class="docutils literal notranslate"><span class="pre">timedelta(0)</span></code>.</p>
<p>Instance methods:</p>
<dl class="method">
<dt id="datetime.timedelta.total_seconds">
<code class="descclassname">timedelta.</code><code class="descname">total_seconds</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.timedelta.total_seconds" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the total number of seconds contained in the duration. Equivalent to
<code class="docutils literal notranslate"><span class="pre">td</span> <span class="pre">/</span> <span class="pre">timedelta(seconds=1)</span></code>.</p>
<p>Note that for very large time intervals (greater than 270 years on
most platforms) this method will lose microsecond accuracy.</p>
<div class="versionadded">
<p><span class="versionmodified">3.2 版新加入.</span></p>
</div>
</dd></dl>
<p>Example usage:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">timedelta</span>
<span class="gp">>>> </span><span class="n">year</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">365</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">another_year</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">weeks</span><span class="o">=</span><span class="mi">40</span><span class="p">,</span> <span class="n">days</span><span class="o">=</span><span class="mi">84</span><span class="p">,</span> <span class="n">hours</span><span class="o">=</span><span class="mi">23</span><span class="p">,</span>
<span class="gp">... </span> <span class="n">minutes</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">seconds</span><span class="o">=</span><span class="mi">600</span><span class="p">)</span> <span class="c1"># adds up to 365 days</span>
<span class="gp">>>> </span><span class="n">year</span><span class="o">.</span><span class="n">total_seconds</span><span class="p">()</span>
<span class="go">31536000.0</span>
<span class="gp">>>> </span><span class="n">year</span> <span class="o">==</span> <span class="n">another_year</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">ten_years</span> <span class="o">=</span> <span class="mi">10</span> <span class="o">*</span> <span class="n">year</span>
<span class="gp">>>> </span><span class="n">ten_years</span><span class="p">,</span> <span class="n">ten_years</span><span class="o">.</span><span class="n">days</span> <span class="o">//</span> <span class="mi">365</span>
<span class="go">(datetime.timedelta(days=3650), 10)</span>
<span class="gp">>>> </span><span class="n">nine_years</span> <span class="o">=</span> <span class="n">ten_years</span> <span class="o">-</span> <span class="n">year</span>
<span class="gp">>>> </span><span class="n">nine_years</span><span class="p">,</span> <span class="n">nine_years</span><span class="o">.</span><span class="n">days</span> <span class="o">//</span> <span class="mi">365</span>
<span class="go">(datetime.timedelta(days=3285), 9)</span>
<span class="gp">>>> </span><span class="n">three_years</span> <span class="o">=</span> <span class="n">nine_years</span> <span class="o">//</span> <span class="mi">3</span>
<span class="gp">>>> </span><span class="n">three_years</span><span class="p">,</span> <span class="n">three_years</span><span class="o">.</span><span class="n">days</span> <span class="o">//</span> <span class="mi">365</span>
<span class="go">(datetime.timedelta(days=1095), 3)</span>
<span class="gp">>>> </span><span class="nb">abs</span><span class="p">(</span><span class="n">three_years</span> <span class="o">-</span> <span class="n">ten_years</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">three_years</span> <span class="o">+</span> <span class="n">year</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="date-objects">
<span id="datetime-date"></span><h2>8.1.3. <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> Objects<a class="headerlink" href="#date-objects" title="本標題的永久連結">¶</a></h2>
<p>A <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> object represents a date (year, month and day) in an idealized
calendar, the current Gregorian calendar indefinitely extended in both
directions. January 1 of year 1 is called day number 1, January 2 of year 1 is
called day number 2, and so on. This matches the definition of the 「proleptic
Gregorian」 calendar in Dershowitz and Reingold’s book Calendrical Calculations,
where it’s the base calendar for all computations. See the book for algorithms
for converting between proleptic Gregorian ordinals and many other calendar
systems.</p>
<dl class="class">
<dt id="datetime.date">
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">date</code><span class="sig-paren">(</span><em>year</em>, <em>month</em>, <em>day</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date" title="本定義的永久連結">¶</a></dt>
<dd><p>All arguments are required. Arguments may be integers, in the following
ranges:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">MINYEAR</span> <span class="pre"><=</span> <span class="pre">year</span> <span class="pre"><=</span> <span class="pre">MAXYEAR</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre"><=</span> <span class="pre">month</span> <span class="pre"><=</span> <span class="pre">12</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre"><=</span> <span class="pre">day</span> <span class="pre"><=</span> <span class="pre">number</span> <span class="pre">of</span> <span class="pre">days</span> <span class="pre">in</span> <span class="pre">the</span> <span class="pre">given</span> <span class="pre">month</span> <span class="pre">and</span> <span class="pre">year</span></code></li>
</ul>
<p>If an argument outside those ranges is given, <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p>
</dd></dl>
<p>Other constructors, all class methods:</p>
<dl class="classmethod">
<dt id="datetime.date.today">
<em class="property">classmethod </em><code class="descclassname">date.</code><code class="descname">today</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.today" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the current local date. This is equivalent to
<code class="docutils literal notranslate"><span class="pre">date.fromtimestamp(time.time())</span></code>.</p>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.date.fromtimestamp">
<em class="property">classmethod </em><code class="descclassname">date.</code><code class="descname">fromtimestamp</code><span class="sig-paren">(</span><em>timestamp</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.fromtimestamp" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the local date corresponding to the POSIX timestamp, such as is returned
by <a class="reference internal" href="time.html#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.time()</span></code></a>. This may raise <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a>, if the timestamp is out
of the range of values supported by the platform C <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> function,
and <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> on <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> failure.
It’s common for this to be restricted to years from 1970 through 2038. Note
that on non-POSIX systems that include leap seconds in their notion of a
timestamp, leap seconds are ignored by <a class="reference internal" href="#datetime.date.fromtimestamp" title="datetime.date.fromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromtimestamp()</span></code></a>.</p>
<div class="versionchanged">
<p><span class="versionmodified">3.3 版更變: </span>Raise <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> instead of <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> if the timestamp
is out of the range of values supported by the platform C
<code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> function. Raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> instead of
<a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> on <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> failure.</p>
</div>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.date.fromordinal">
<em class="property">classmethod </em><code class="descclassname">date.</code><code class="descname">fromordinal</code><span class="sig-paren">(</span><em>ordinal</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.fromordinal" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the date corresponding to the proleptic Gregorian ordinal, where January
1 of year 1 has ordinal 1. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised unless <code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre"><=</span> <span class="pre">ordinal</span> <span class="pre"><=</span>
<span class="pre">date.max.toordinal()</span></code>. For any date <em>d</em>, <code class="docutils literal notranslate"><span class="pre">date.fromordinal(d.toordinal())</span> <span class="pre">==</span>
<span class="pre">d</span></code>.</p>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.date.fromisoformat">
<em class="property">classmethod </em><code class="descclassname">date.</code><code class="descname">fromisoformat</code><span class="sig-paren">(</span><em>date_string</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.fromisoformat" title="本定義的永久連結">¶</a></dt>
<dd><p>Return a <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> corresponding to a <em>date_string</em> in the format emitted
by <a class="reference internal" href="#datetime.date.isoformat" title="datetime.date.isoformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">date.isoformat()</span></code></a>. Specifically, this function supports strings in
the format(s) <code class="docutils literal notranslate"><span class="pre">YYYY-MM-DD</span></code>.</p>
<div class="admonition caution">
<p class="first admonition-title">警示</p>
<p class="last">This does not support parsing arbitrary ISO 8601 strings - it is only intended
as the inverse operation of <a class="reference internal" href="#datetime.date.isoformat" title="datetime.date.isoformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">date.isoformat()</span></code></a>.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">3.7 版新加入.</span></p>
</div>
</dd></dl>
<p>Class attributes:</p>
<dl class="attribute">
<dt id="datetime.date.min">
<code class="descclassname">date.</code><code class="descname">min</code><a class="headerlink" href="#datetime.date.min" title="本定義的永久連結">¶</a></dt>
<dd><p>The earliest representable date, <code class="docutils literal notranslate"><span class="pre">date(MINYEAR,</span> <span class="pre">1,</span> <span class="pre">1)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.date.max">
<code class="descclassname">date.</code><code class="descname">max</code><a class="headerlink" href="#datetime.date.max" title="本定義的永久連結">¶</a></dt>
<dd><p>The latest representable date, <code class="docutils literal notranslate"><span class="pre">date(MAXYEAR,</span> <span class="pre">12,</span> <span class="pre">31)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.date.resolution">
<code class="descclassname">date.</code><code class="descname">resolution</code><a class="headerlink" href="#datetime.date.resolution" title="本定義的永久連結">¶</a></dt>
<dd><p>The smallest possible difference between non-equal date objects,
<code class="docutils literal notranslate"><span class="pre">timedelta(days=1)</span></code>.</p>
</dd></dl>
<p>Instance attributes (read-only):</p>
<dl class="attribute">
<dt id="datetime.date.year">
<code class="descclassname">date.</code><code class="descname">year</code><a class="headerlink" href="#datetime.date.year" title="本定義的永久連結">¶</a></dt>
<dd><p>Between <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MINYEAR</span></code></a> and <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAXYEAR</span></code></a> inclusive.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.date.month">
<code class="descclassname">date.</code><code class="descname">month</code><a class="headerlink" href="#datetime.date.month" title="本定義的永久連結">¶</a></dt>
<dd><p>Between 1 and 12 inclusive.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.date.day">
<code class="descclassname">date.</code><code class="descname">day</code><a class="headerlink" href="#datetime.date.day" title="本定義的永久連結">¶</a></dt>
<dd><p>Between 1 and the number of days in the given month of the given year.</p>
</dd></dl>
<p>Supported operations:</p>
<table border="1" class="docutils">
<colgroup>
<col width="40%" />
<col width="60%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Operation</th>
<th class="head">Result</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">date2</span> <span class="pre">=</span> <span class="pre">date1</span> <span class="pre">+</span> <span class="pre">timedelta</span></code></td>
<td><em>date2</em> is <code class="docutils literal notranslate"><span class="pre">timedelta.days</span></code> days removed
from <em>date1</em>. (1)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">date2</span> <span class="pre">=</span> <span class="pre">date1</span> <span class="pre">-</span> <span class="pre">timedelta</span></code></td>
<td>Computes <em>date2</em> such that <code class="docutils literal notranslate"><span class="pre">date2</span> <span class="pre">+</span>
<span class="pre">timedelta</span> <span class="pre">==</span> <span class="pre">date1</span></code>. (2)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">timedelta</span> <span class="pre">=</span> <span class="pre">date1</span> <span class="pre">-</span> <span class="pre">date2</span></code></td>
<td>(3)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">date1</span> <span class="pre"><</span> <span class="pre">date2</span></code></td>
<td><em>date1</em> is considered less than <em>date2</em> when
<em>date1</em> precedes <em>date2</em> in time. (4)</td>
</tr>
</tbody>
</table>
<p>註解:</p>
<ol class="arabic simple">
<li><em>date2</em> is moved forward in time if <code class="docutils literal notranslate"><span class="pre">timedelta.days</span> <span class="pre">></span> <span class="pre">0</span></code>, or backward if
<code class="docutils literal notranslate"><span class="pre">timedelta.days</span> <span class="pre"><</span> <span class="pre">0</span></code>. Afterward <code class="docutils literal notranslate"><span class="pre">date2</span> <span class="pre">-</span> <span class="pre">date1</span> <span class="pre">==</span> <span class="pre">timedelta.days</span></code>.
<code class="docutils literal notranslate"><span class="pre">timedelta.seconds</span></code> and <code class="docutils literal notranslate"><span class="pre">timedelta.microseconds</span></code> are ignored.
<a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> is raised if <code class="docutils literal notranslate"><span class="pre">date2.year</span></code> would be smaller than
<a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MINYEAR</span></code></a> or larger than <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAXYEAR</span></code></a>.</li>
<li><code class="docutils literal notranslate"><span class="pre">timedelta.seconds</span></code> and <code class="docutils literal notranslate"><span class="pre">timedelta.microseconds</span></code> are ignored.</li>
<li>This is exact, and cannot overflow. timedelta.seconds and
timedelta.microseconds are 0, and date2 + timedelta == date1 after.</li>
<li>In other words, <code class="docutils literal notranslate"><span class="pre">date1</span> <span class="pre"><</span> <span class="pre">date2</span></code> if and only if <code class="docutils literal notranslate"><span class="pre">date1.toordinal()</span> <span class="pre"><</span>
<span class="pre">date2.toordinal()</span></code>. In order to stop comparison from falling back to the
default scheme of comparing object addresses, date comparison normally raises
<a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if the other comparand isn’t also a <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> object.
However, <code class="docutils literal notranslate"><span class="pre">NotImplemented</span></code> is returned instead if the other comparand has a
<code class="xref py py-meth docutils literal notranslate"><span class="pre">timetuple()</span></code> attribute. This hook gives other kinds of date objects a
chance at implementing mixed-type comparison. If not, when a <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a>
object is compared to an object of a different type, <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised
unless the comparison is <code class="docutils literal notranslate"><span class="pre">==</span></code> or <code class="docutils literal notranslate"><span class="pre">!=</span></code>. The latter cases return
<a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a> or <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a>, respectively.</li>
</ol>
<p>Dates can be used as dictionary keys. In Boolean contexts, all <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a>
objects are considered to be true.</p>
<p>Instance methods:</p>
<dl class="method">
<dt id="datetime.date.replace">
<code class="descclassname">date.</code><code class="descname">replace</code><span class="sig-paren">(</span><em>year=self.year</em>, <em>month=self.month</em>, <em>day=self.day</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.replace" title="本定義的永久連結">¶</a></dt>
<dd><p>Return a date with the same value, except for those parameters given new
values by whichever keyword arguments are specified. For example, if <code class="docutils literal notranslate"><span class="pre">d</span> <span class="pre">==</span>
<span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">31)</span></code>, then <code class="docutils literal notranslate"><span class="pre">d.replace(day=26)</span> <span class="pre">==</span> <span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">26)</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.timetuple">
<code class="descclassname">date.</code><code class="descname">timetuple</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.timetuple" title="本定義的永久連結">¶</a></dt>
<dd><p>Return a <a class="reference internal" href="time.html#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time.struct_time</span></code></a> such as returned by <a class="reference internal" href="time.html#time.localtime" title="time.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.localtime()</span></code></a>.
The hours, minutes and seconds are 0, and the DST flag is -1. <code class="docutils literal notranslate"><span class="pre">d.timetuple()</span></code>
is equivalent to <code class="docutils literal notranslate"><span class="pre">time.struct_time((d.year,</span> <span class="pre">d.month,</span> <span class="pre">d.day,</span> <span class="pre">0,</span> <span class="pre">0,</span> <span class="pre">0,</span>
<span class="pre">d.weekday(),</span> <span class="pre">yday,</span> <span class="pre">-1))</span></code>, where <code class="docutils literal notranslate"><span class="pre">yday</span> <span class="pre">=</span> <span class="pre">d.toordinal()</span> <span class="pre">-</span> <span class="pre">date(d.year,</span> <span class="pre">1,</span>
<span class="pre">1).toordinal()</span> <span class="pre">+</span> <span class="pre">1</span></code> is the day number within the current year starting with
<code class="docutils literal notranslate"><span class="pre">1</span></code> for January 1st.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.toordinal">
<code class="descclassname">date.</code><code class="descname">toordinal</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.toordinal" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the proleptic Gregorian ordinal of the date, where January 1 of year 1
has ordinal 1. For any <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> object <em>d</em>,
<code class="docutils literal notranslate"><span class="pre">date.fromordinal(d.toordinal())</span> <span class="pre">==</span> <span class="pre">d</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.weekday">
<code class="descclassname">date.</code><code class="descname">weekday</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.weekday" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the day of the week as an integer, where Monday is 0 and Sunday is 6.
For example, <code class="docutils literal notranslate"><span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">4).weekday()</span> <span class="pre">==</span> <span class="pre">2</span></code>, a Wednesday. See also
<a class="reference internal" href="#datetime.date.isoweekday" title="datetime.date.isoweekday"><code class="xref py py-meth docutils literal notranslate"><span class="pre">isoweekday()</span></code></a>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.isoweekday">
<code class="descclassname">date.</code><code class="descname">isoweekday</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.isoweekday" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the day of the week as an integer, where Monday is 1 and Sunday is 7.
For example, <code class="docutils literal notranslate"><span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">4).isoweekday()</span> <span class="pre">==</span> <span class="pre">3</span></code>, a Wednesday. See also
<a class="reference internal" href="#datetime.date.weekday" title="datetime.date.weekday"><code class="xref py py-meth docutils literal notranslate"><span class="pre">weekday()</span></code></a>, <a class="reference internal" href="#datetime.date.isocalendar" title="datetime.date.isocalendar"><code class="xref py py-meth docutils literal notranslate"><span class="pre">isocalendar()</span></code></a>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.isocalendar">
<code class="descclassname">date.</code><code class="descname">isocalendar</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.isocalendar" title="本定義的永久連結">¶</a></dt>
<dd><p>Return a 3-tuple, (ISO year, ISO week number, ISO weekday).</p>
<p>The ISO calendar is a widely used variant of the Gregorian calendar. See
<a class="reference external" href="https://www.staff.science.uu.nl/~gent0113/calendar/isocalendar.htm">https://www.staff.science.uu.nl/~gent0113/calendar/isocalendar.htm</a> for a good
explanation.</p>
<p>The ISO year consists of 52 or 53 full weeks, and where a week starts on a
Monday and ends on a Sunday. The first week of an ISO year is the first
(Gregorian) calendar week of a year containing a Thursday. This is called week
number 1, and the ISO year of that Thursday is the same as its Gregorian year.</p>
<p>For example, 2004 begins on a Thursday, so the first week of ISO year 2004
begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan 2004, so that
<code class="docutils literal notranslate"><span class="pre">date(2003,</span> <span class="pre">12,</span> <span class="pre">29).isocalendar()</span> <span class="pre">==</span> <span class="pre">(2004,</span> <span class="pre">1,</span> <span class="pre">1)</span></code> and <code class="docutils literal notranslate"><span class="pre">date(2004,</span> <span class="pre">1,</span>
<span class="pre">4).isocalendar()</span> <span class="pre">==</span> <span class="pre">(2004,</span> <span class="pre">1,</span> <span class="pre">7)</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.isoformat">
<code class="descclassname">date.</code><code class="descname">isoformat</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.isoformat" title="本定義的永久連結">¶</a></dt>
<dd><p>Return a string representing the date in ISO 8601 format, 『YYYY-MM-DD』. For
example, <code class="docutils literal notranslate"><span class="pre">date(2002,</span> <span class="pre">12,</span> <span class="pre">4).isoformat()</span> <span class="pre">==</span> <span class="pre">'2002-12-04'</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.__str__">
<code class="descclassname">date.</code><code class="descname">__str__</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.__str__" title="本定義的永久連結">¶</a></dt>
<dd><p>For a date <em>d</em>, <code class="docutils literal notranslate"><span class="pre">str(d)</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">d.isoformat()</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.ctime">
<code class="descclassname">date.</code><code class="descname">ctime</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.ctime" title="本定義的永久連結">¶</a></dt>
<dd><p>Return a string representing the date, for example <code class="docutils literal notranslate"><span class="pre">date(2002,</span> <span class="pre">12,</span>
<span class="pre">4).ctime()</span> <span class="pre">==</span> <span class="pre">'Wed</span> <span class="pre">Dec</span> <span class="pre">4</span> <span class="pre">00:00:00</span> <span class="pre">2002'</span></code>. <code class="docutils literal notranslate"><span class="pre">d.ctime()</span></code> is equivalent to
<code class="docutils literal notranslate"><span class="pre">time.ctime(time.mktime(d.timetuple()))</span></code> on platforms where the native C
<code class="xref c c-func docutils literal notranslate"><span class="pre">ctime()</span></code> function (which <a class="reference internal" href="time.html#time.ctime" title="time.ctime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.ctime()</span></code></a> invokes, but which
<a class="reference internal" href="#datetime.date.ctime" title="datetime.date.ctime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">date.ctime()</span></code></a> does not invoke) conforms to the C standard.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.strftime">
<code class="descclassname">date.</code><code class="descname">strftime</code><span class="sig-paren">(</span><em>format</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.strftime" title="本定義的永久連結">¶</a></dt>
<dd><p>Return a string representing the date, controlled by an explicit format string.
Format codes referring to hours, minutes or seconds will see 0 values. For a
complete list of formatting directives, see
<a class="reference internal" href="#strftime-strptime-behavior"><span class="std std-ref">strftime() and strptime() Behavior</span></a>.</p>
</dd></dl>
<dl class="method">
<dt id="datetime.date.__format__">
<code class="descclassname">date.</code><code class="descname">__format__</code><span class="sig-paren">(</span><em>format</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.date.__format__" title="本定義的永久連結">¶</a></dt>
<dd><p>Same as <a class="reference internal" href="#datetime.date.strftime" title="datetime.date.strftime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">date.strftime()</span></code></a>. This makes it possible to specify a format
string for a <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> object in <a class="reference internal" href="../reference/lexical_analysis.html#f-strings"><span class="std std-ref">formatted string
literals</span></a> and when using <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a>. For a
complete list of formatting directives, see
<a class="reference internal" href="#strftime-strptime-behavior"><span class="std std-ref">strftime() and strptime() Behavior</span></a>.</p>
</dd></dl>
<p>Example of counting days to an event:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">time</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">date</span>
<span class="gp">>>> </span><span class="n">today</span> <span class="o">=</span> <span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">today</span>
<span class="go">datetime.date(2007, 12, 5)</span>
<span class="gp">>>> </span><span class="n">today</span> <span class="o">==</span> <span class="n">date</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">my_birthday</span> <span class="o">=</span> <span class="n">date</span><span class="p">(</span><span class="n">today</span><span class="o">.</span><span class="n">year</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">24</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">if</span> <span class="n">my_birthday</span> <span class="o"><</span> <span class="n">today</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">my_birthday</span> <span class="o">=</span> <span class="n">my_birthday</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="n">today</span><span class="o">.</span><span class="n">year</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">my_birthday</span>
<span class="go">datetime.date(2008, 6, 24)</span>
<span class="gp">>>> </span><span class="n">time_to_birthday</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">my_birthday</span> <span class="o">-</span> <span class="n">today</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">time_to_birthday</span><span class="o">.</span><span class="n">days</span>
<span class="go">202</span>
</pre></div>
</div>
<p>Example of working with <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a>:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">date</span>
<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">date</span><span class="o">.</span><span class="n">fromordinal</span><span class="p">(</span><span class="mi">730920</span><span class="p">)</span> <span class="c1"># 730920th day after 1. 1. 0001</span>
<span class="gp">>>> </span><span class="n">d</span>
<span class="go">datetime.date(2002, 3, 11)</span>
<span class="gp">>>> </span><span class="n">t</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">timetuple</span><span class="p">()</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">t</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="go">2002 # year</span>
<span class="go">3 # month</span>
<span class="go">11 # day</span>
<span class="go">0</span>
<span class="go">0</span>
<span class="go">0</span>
<span class="go">0 # weekday (0 = Monday)</span>
<span class="go">70 # 70th day in the year</span>
<span class="go">-1</span>
<span class="gp">>>> </span><span class="n">ic</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">isocalendar</span><span class="p">()</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">ic</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="go">2002 # ISO year</span>
<span class="go">11 # ISO week number</span>
<span class="go">1 # ISO day number ( 1 = Monday )</span>
<span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span>
<span class="go">'2002-03-11'</span>
<span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">"</span><span class="si">%d</span><span class="s2">/%m/%y"</span><span class="p">)</span>
<span class="go">'11/03/02'</span>
<span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">"%A </span><span class="si">%d</span><span class="s2">. %B %Y"</span><span class="p">)</span>
<span class="go">'Monday 11. March 2002'</span>
<span class="gp">>>> </span><span class="s1">'The {1} is {0:</span><span class="si">%d</span><span class="s1">}, the {2} is {0:%B}.'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="s2">"day"</span><span class="p">,</span> <span class="s2">"month"</span><span class="p">)</span>
<span class="go">'The day is 11, the month is March.'</span>
</pre></div>
</div>
</div>
<div class="section" id="datetime-objects">
<span id="datetime-datetime"></span><h2>8.1.4. <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> Objects<a class="headerlink" href="#datetime-objects" title="本標題的永久連結">¶</a></h2>
<p>A <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object is a single object containing all the information
from a <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> object and a <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> object. Like a <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a>
object, <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> assumes the current Gregorian calendar extended in
both directions; like a time object, <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> assumes there are exactly
3600*24 seconds in every day.</p>
<p>Constructor:</p>
<dl class="class">
<dt id="datetime.datetime">
<em class="property">class </em><code class="descclassname">datetime.</code><code class="descname">datetime</code><span class="sig-paren">(</span><em>year</em>, <em>month</em>, <em>day</em>, <em>hour=0</em>, <em>minute=0</em>, <em>second=0</em>, <em>microsecond=0</em>, <em>tzinfo=None</em>, <em>*</em>, <em>fold=0</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime" title="本定義的永久連結">¶</a></dt>
<dd><p>The year, month and day arguments are required. <em>tzinfo</em> may be <code class="docutils literal notranslate"><span class="pre">None</span></code>, or an
instance of a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> subclass. The remaining arguments may be integers,
in the following ranges:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">MINYEAR</span> <span class="pre"><=</span> <span class="pre">year</span> <span class="pre"><=</span> <span class="pre">MAXYEAR</span></code>,</li>
<li><code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre"><=</span> <span class="pre">month</span> <span class="pre"><=</span> <span class="pre">12</span></code>,</li>
<li><code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre"><=</span> <span class="pre">day</span> <span class="pre"><=</span> <span class="pre">number</span> <span class="pre">of</span> <span class="pre">days</span> <span class="pre">in</span> <span class="pre">the</span> <span class="pre">given</span> <span class="pre">month</span> <span class="pre">and</span> <span class="pre">year</span></code>,</li>
<li><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">hour</span> <span class="pre"><</span> <span class="pre">24</span></code>,</li>
<li><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">minute</span> <span class="pre"><</span> <span class="pre">60</span></code>,</li>
<li><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">second</span> <span class="pre"><</span> <span class="pre">60</span></code>,</li>
<li><code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">microsecond</span> <span class="pre"><</span> <span class="pre">1000000</span></code>,</li>
<li><code class="docutils literal notranslate"><span class="pre">fold</span> <span class="pre">in</span> <span class="pre">[0,</span> <span class="pre">1]</span></code>.</li>
</ul>
<p>If an argument outside those ranges is given, <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p>
<div class="versionadded">
<p><span class="versionmodified">3.6 版新加入: </span>Added the <code class="docutils literal notranslate"><span class="pre">fold</span></code> argument.</p>
</div>
</dd></dl>
<p>Other constructors, all class methods:</p>
<dl class="classmethod">
<dt id="datetime.datetime.today">
<em class="property">classmethod </em><code class="descclassname">datetime.</code><code class="descname">today</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.today" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the current local datetime, with <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> <code class="docutils literal notranslate"><span class="pre">None</span></code>. This is
equivalent to <code class="docutils literal notranslate"><span class="pre">datetime.fromtimestamp(time.time())</span></code>. See also <a class="reference internal" href="#datetime.datetime.now" title="datetime.datetime.now"><code class="xref py py-meth docutils literal notranslate"><span class="pre">now()</span></code></a>,
<a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromtimestamp()</span></code></a>.</p>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.datetime.now">
<em class="property">classmethod </em><code class="descclassname">datetime.</code><code class="descname">now</code><span class="sig-paren">(</span><em>tz=None</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.now" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the current local date and time. If optional argument <em>tz</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>
or not specified, this is like <a class="reference internal" href="#datetime.datetime.today" title="datetime.datetime.today"><code class="xref py py-meth docutils literal notranslate"><span class="pre">today()</span></code></a>, but, if possible, supplies more
precision than can be gotten from going through a <a class="reference internal" href="time.html#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.time()</span></code></a> timestamp
(for example, this may be possible on platforms supplying the C
<code class="xref c c-func docutils literal notranslate"><span class="pre">gettimeofday()</span></code> function).</p>
<p>If <em>tz</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, it must be an instance of a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> subclass, and the
current date and time are converted to <em>tz</em>’s time zone. In this case the
result is equivalent to <code class="docutils literal notranslate"><span class="pre">tz.fromutc(datetime.utcnow().replace(tzinfo=tz))</span></code>.
See also <a class="reference internal" href="#datetime.datetime.today" title="datetime.datetime.today"><code class="xref py py-meth docutils literal notranslate"><span class="pre">today()</span></code></a>, <a class="reference internal" href="#datetime.datetime.utcnow" title="datetime.datetime.utcnow"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcnow()</span></code></a>.</p>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.datetime.utcnow">
<em class="property">classmethod </em><code class="descclassname">datetime.</code><code class="descname">utcnow</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.utcnow" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the current UTC date and time, with <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> <code class="docutils literal notranslate"><span class="pre">None</span></code>. This is like
<a class="reference internal" href="#datetime.datetime.now" title="datetime.datetime.now"><code class="xref py py-meth docutils literal notranslate"><span class="pre">now()</span></code></a>, but returns the current UTC date and time, as a naive
<a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object. An aware current UTC datetime can be obtained by
calling <code class="docutils literal notranslate"><span class="pre">datetime.now(timezone.utc)</span></code>. See also <a class="reference internal" href="#datetime.datetime.now" title="datetime.datetime.now"><code class="xref py py-meth docutils literal notranslate"><span class="pre">now()</span></code></a>.</p>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.datetime.fromtimestamp">
<em class="property">classmethod </em><code class="descclassname">datetime.</code><code class="descname">fromtimestamp</code><span class="sig-paren">(</span><em>timestamp</em>, <em>tz=None</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.fromtimestamp" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the local date and time corresponding to the POSIX timestamp, such as is
returned by <a class="reference internal" href="time.html#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.time()</span></code></a>. If optional argument <em>tz</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> or not
specified, the timestamp is converted to the platform’s local date and time, and
the returned <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object is naive.</p>
<p>If <em>tz</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, it must be an instance of a <a class="reference internal" href="#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> subclass, and the
timestamp is converted to <em>tz</em>’s time zone. In this case the result is
equivalent to
<code class="docutils literal notranslate"><span class="pre">tz.fromutc(datetime.utcfromtimestamp(timestamp).replace(tzinfo=tz))</span></code>.</p>
<p><a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromtimestamp()</span></code></a> may raise <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a>, if the timestamp is out of
the range of values supported by the platform C <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> or
<code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> functions, and <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> on <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> or
<code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> failure.
It’s common for this to be restricted to years in
1970 through 2038. Note that on non-POSIX systems that include leap seconds in
their notion of a timestamp, leap seconds are ignored by <a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromtimestamp()</span></code></a>,
and then it’s possible to have two timestamps differing by a second that yield
identical <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> objects. See also <a class="reference internal" href="#datetime.datetime.utcfromtimestamp" title="datetime.datetime.utcfromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">utcfromtimestamp()</span></code></a>.</p>
<div class="versionchanged">
<p><span class="versionmodified">3.3 版更變: </span>Raise <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> instead of <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> if the timestamp
is out of the range of values supported by the platform C
<code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> or <code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> functions. Raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>
instead of <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> on <code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> or <code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code>
failure.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">3.6 版更變: </span><a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromtimestamp()</span></code></a> may return instances with <a class="reference internal" href="#datetime.datetime.fold" title="datetime.datetime.fold"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fold</span></code></a> set to 1.</p>
</div>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.datetime.utcfromtimestamp">
<em class="property">classmethod </em><code class="descclassname">datetime.</code><code class="descname">utcfromtimestamp</code><span class="sig-paren">(</span><em>timestamp</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.utcfromtimestamp" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the UTC <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> corresponding to the POSIX timestamp, with
<a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> <code class="docutils literal notranslate"><span class="pre">None</span></code>. This may raise <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a>, if the timestamp is
out of the range of values supported by the platform C <code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> function,
and <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> on <code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> failure.
It’s common for this to be restricted to years in 1970 through 2038.</p>
<p>To get an aware <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object, call <a class="reference internal" href="#datetime.datetime.fromtimestamp" title="datetime.datetime.fromtimestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromtimestamp()</span></code></a>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">datetime</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="n">timestamp</span><span class="p">,</span> <span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
</pre></div>
</div>
<p>On the POSIX compliant platforms, it is equivalent to the following
expression:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">datetime</span><span class="p">(</span><span class="mi">1970</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span> <span class="o">+</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">seconds</span><span class="o">=</span><span class="n">timestamp</span><span class="p">)</span>
</pre></div>
</div>
<p>except the latter formula always supports the full years range: between
<a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MINYEAR</span></code></a> and <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAXYEAR</span></code></a> inclusive.</p>
<div class="versionchanged">
<p><span class="versionmodified">3.3 版更變: </span>Raise <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> instead of <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> if the timestamp
is out of the range of values supported by the platform C
<code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> function. Raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> instead of
<a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> on <code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code> failure.</p>
</div>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.datetime.fromordinal">
<em class="property">classmethod </em><code class="descclassname">datetime.</code><code class="descname">fromordinal</code><span class="sig-paren">(</span><em>ordinal</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.fromordinal" title="本定義的永久連結">¶</a></dt>
<dd><p>Return the <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> corresponding to the proleptic Gregorian ordinal,
where January 1 of year 1 has ordinal 1. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised unless <code class="docutils literal notranslate"><span class="pre">1</span>
<span class="pre"><=</span> <span class="pre">ordinal</span> <span class="pre"><=</span> <span class="pre">datetime.max.toordinal()</span></code>. The hour, minute, second and
microsecond of the result are all 0, and <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.datetime.combine">
<em class="property">classmethod </em><code class="descclassname">datetime.</code><code class="descname">combine</code><span class="sig-paren">(</span><em>date</em>, <em>time</em>, <em>tzinfo=self.tzinfo</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.combine" title="本定義的永久連結">¶</a></dt>
<dd><p>Return a new <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object whose date components are equal to the
given <a class="reference internal" href="#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> object’s, and whose time components
are equal to the given <a class="reference internal" href="#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time</span></code></a> object’s. If the <em>tzinfo</em>
argument is provided, its value is used to set the <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> attribute
of the result, otherwise the <a class="reference internal" href="#datetime.time.tzinfo" title="datetime.time.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> attribute of the <em>time</em> argument
is used.</p>
<p>For any <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object <em>d</em>,
<code class="docutils literal notranslate"><span class="pre">d</span> <span class="pre">==</span> <span class="pre">datetime.combine(d.date(),</span> <span class="pre">d.time(),</span> <span class="pre">d.tzinfo)</span></code>. If date is a
<a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> object, its time components and <a class="reference internal" href="#datetime.datetime.tzinfo" title="datetime.datetime.tzinfo"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tzinfo</span></code></a> attributes
are ignored.</p>
<div class="versionchanged">
<p><span class="versionmodified">3.6 版更變: </span>Added the <em>tzinfo</em> argument.</p>
</div>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.datetime.fromisoformat">
<em class="property">classmethod </em><code class="descclassname">datetime.</code><code class="descname">fromisoformat</code><span class="sig-paren">(</span><em>date_string</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.fromisoformat" title="本定義的永久連結">¶</a></dt>
<dd><p>Return a <a class="reference internal" href="#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> corresponding to a <em>date_string</em> in one of the
formats emitted by <a class="reference internal" href="#datetime.date.isoformat" title="datetime.date.isoformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">date.isoformat()</span></code></a> and <a class="reference internal" href="#datetime.datetime.isoformat" title="datetime.datetime.isoformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.isoformat()</span></code></a>.
Specifically, this function supports strings in the format(s)
<code class="docutils literal notranslate"><span class="pre">YYYY-MM-DD[*HH[:MM[:SS[.mmm[mmm]]]][+HH:MM[:SS[.ffffff]]]]</span></code>,
where <code class="docutils literal notranslate"><span class="pre">*</span></code> can match any single character.</p>
<div class="admonition caution">
<p class="first admonition-title">警示</p>
<p class="last">This does not support parsing arbitrary ISO 8601 strings - it is only intended
as the inverse operation of <a class="reference internal" href="#datetime.datetime.isoformat" title="datetime.datetime.isoformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.isoformat()</span></code></a>.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">3.7 版新加入.</span></p>
</div>
</dd></dl>
<dl class="classmethod">
<dt id="datetime.datetime.strptime">
<em class="property">classmethod </em><code class="descclassname">datetime.</code><code class="descname">strptime</code><span class="sig-paren">(</span><em>date_string</em>, <em>format</em><span class="sig-paren">)</span><a class="headerlink" href="#datetime.datetime.strptime" title="本定義的永久連結">¶</a></dt>
<dd><p>Return a <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> corresponding to <em>date_string</em>, parsed according to
<em>format</em>. This is equivalent to <code class="docutils literal notranslate"><span class="pre">datetime(*(time.strptime(date_string,</span>
<span class="pre">format)[0:6]))</span></code>. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised if the date_string and format
can’t be parsed by <a class="reference internal" href="time.html#time.strptime" title="time.strptime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strptime()</span></code></a> or if it returns a value which isn’t a
time tuple. For a complete list of formatting directives, see
<a class="reference internal" href="#strftime-strptime-behavior"><span class="std std-ref">strftime() and strptime() Behavior</span></a>.</p>
</dd></dl>
<p>Class attributes:</p>
<dl class="attribute">
<dt id="datetime.datetime.min">
<code class="descclassname">datetime.</code><code class="descname">min</code><a class="headerlink" href="#datetime.datetime.min" title="本定義的永久連結">¶</a></dt>
<dd><p>The earliest representable <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>, <code class="docutils literal notranslate"><span class="pre">datetime(MINYEAR,</span> <span class="pre">1,</span> <span class="pre">1,</span>
<span class="pre">tzinfo=None)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.datetime.max">
<code class="descclassname">datetime.</code><code class="descname">max</code><a class="headerlink" href="#datetime.datetime.max" title="本定義的永久連結">¶</a></dt>
<dd><p>The latest representable <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>, <code class="docutils literal notranslate"><span class="pre">datetime(MAXYEAR,</span> <span class="pre">12,</span> <span class="pre">31,</span> <span class="pre">23,</span> <span class="pre">59,</span>
<span class="pre">59,</span> <span class="pre">999999,</span> <span class="pre">tzinfo=None)</span></code>.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.datetime.resolution">
<code class="descclassname">datetime.</code><code class="descname">resolution</code><a class="headerlink" href="#datetime.datetime.resolution" title="本定義的永久連結">¶</a></dt>
<dd><p>The smallest possible difference between non-equal <a class="reference internal" href="#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> objects,
<code class="docutils literal notranslate"><span class="pre">timedelta(microseconds=1)</span></code>.</p>
</dd></dl>
<p>Instance attributes (read-only):</p>
<dl class="attribute">
<dt id="datetime.datetime.year">
<code class="descclassname">datetime.</code><code class="descname">year</code><a class="headerlink" href="#datetime.datetime.year" title="本定義的永久連結">¶</a></dt>
<dd><p>Between <a class="reference internal" href="#datetime.MINYEAR" title="datetime.MINYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MINYEAR</span></code></a> and <a class="reference internal" href="#datetime.MAXYEAR" title="datetime.MAXYEAR"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAXYEAR</span></code></a> inclusive.</p>
</dd></dl>
<dl class="attribute">
<dt id="datetime.datetime.month">
<code class="descclassname">datetime.</code><code class="descname">month</code><a class="headerlink" href="#datetime.datetime.month" title="本定義的永久連結">¶</a></dt>
<dd><p>Between 1 and 12 inclusive.</p>
</dd></dl>