欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python日期和时间函数

程序员文章站 2024-01-28 12:17:22
...

Python时间和日期操作需要用到datetime和time标准库模块。 
一、time模块 
1.time模块表示时间的方式 
①时间戳 
②格式化的时间字符串 
③以数组的形式表示,即(struct_time),共有九个元素

  • year (four digits, e.g. 1998)
  • month (1-12)
  • day (1-31)
  • hours (0-23)
  • minutes (0-59)
  • seconds (0-59)
  • weekday (0-6, Monday is 0)
  • Julian day (day in the year, 1-366)
  • DST (Daylight Savings Time) flag (-1, 0 or 1) 是否是夏令时

2.常用函数 
①asctime([tuple]) -> string 
将一个struct_time(默认为当时时间),转换成字符串。

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">>>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.asctime()
<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Sun Jul 31 11:10:39 2016'</span>
>>> import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>
>>> thisTime = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2016-07-31 12:12:12"</span>
>>> timeTuple = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.strptime(thisTime, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%Y</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%m</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%d</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%H</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%M</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%S</span>"</span>)
>>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.asctime(timeTuple)
<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Sun Jul 31 12:12:12 2016'</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

②ctime(seconds) -> string 
将一个时间戳(默认为当前时间)转换成一个时间字符串。

<code class="hljs python has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-prompt" style="color: rgb(0, 102, 102); box-sizing: border-box;">>>> </span>time.ctime()
<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Sun Jul 31 11:11:33 2016'</span>
<span class="hljs-prompt" style="color: rgb(0, 102, 102); box-sizing: border-box;">>>> </span>time.ctime(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1469938332</span>)
<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Sun Jul 31 12:12:12 2016'</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

③ gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst) 
将一个时间戳转换成一个UTC时区(0时区)的struct_time,如果seconds参数未输入,则以当前时间为转换标准。

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">>>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">gmtime</span>()
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.struct_time(tm_year=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2016</span>, tm_mon=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>, tm_mday=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">31</span>, tm_hour=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>, tm_min=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>, tm_sec=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span>, tm_wday=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>, tm_yday=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">213</span>, tm_isdst=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)
>>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">gmtime</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1469938332</span>)
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.struct_time(tm_year=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2016</span>, tm_mon=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>, tm_mday=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">31</span>, tm_hour=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>, tm_min=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>, tm_sec=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>, tm_wday=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>, tm_yday=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">213</span>, tm_isdst=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

④localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst) 
将一个时间戳转换成一个当前时区的struct_time,如果seconds参数未输入,则以当前时间为转换标准。

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">>>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">localtime</span>()
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.struct_time(tm_year=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2016</span>, tm_mon=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>, tm_mday=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">31</span>, tm_hour=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11</span>, tm_min=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">17</span>, tm_sec=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">17</span>, tm_wday=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>, tm_yday=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">213</span>, tm_isdst=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)
>>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">localtime</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1469938332</span>)
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.struct_time(tm_year=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2016</span>, tm_mon=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>, tm_mday=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">31</span>, tm_hour=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>, tm_min=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>, tm_sec=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>, tm_wday=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>, tm_yday=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">213</span>, tm_isdst=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

⑤ mktime(tuple) -> floating point number 
将一个以struct_time转换为时间戳。

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">>>> import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>
>>> thisTime = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2016-07-31 12:12:12"</span>
>>> timeTuple = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.strptime(thisTime, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%Y</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%m</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%d</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%H</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%M</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%S</span>"</span>)
>>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.mktime(timeTuple)
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1469938332.0</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

⑥ strftime(format[, tuple]) -> string 
将指定的struct_time(默认为当前时间),根据指定的格式化字符串输出。

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">>>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.strftime(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%Y</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%m</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%d</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%H</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%M</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%S</span>"</span>)
<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'2016-07-31 11:20:41'</span>
>>> import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>
>>> thisTime = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2016-07-31 12:12:12"</span>
>>> timeTuple = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.strptime(thisTime, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%Y</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%m</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%d</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%H</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%M</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%S</span>"</span>)
>>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.strftime(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%Y</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%m</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%d</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%H</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%M</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%S</span>"</span>,timeTuple)
<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'2016-07-31 12:12:12'</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

⑦strptime(string, format) -> struct_time 
将时间字符串根据指定的格式化符转换成数组形式的时间。

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">>>> import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>
>>> thisTime = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2016-07-31 12:12:12"</span>
>>> timeTuple = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.strptime(thisTime, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%Y</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%m</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%d</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%H</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%M</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%S</span>"</span>)
>>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">print</span> timeTuple
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.struct_time(tm_year=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2016</span>, tm_mon=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>, tm_mday=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">31</span>, tm_hour=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>, tm_min=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>, tm_sec=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>, tm_wday=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>, tm_yday=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">213</span>, tm_isdst=-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

⑧time() -> floating point number 
返回当前时间的时间戳。

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">>>> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">time</span>.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">time</span>()
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1469935566.776</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

二、datetime模块 
1.datetime中的常量

  • datetime.MINYEAR,表示datetime所能表示的最小年份,MINYEAR = 1。
  • datetime.MAXYEAR,表示datetime所能表示的最大年份,MAXYEAR = 9999。

2.datetime中的类

  • datetime.date:表示日期的类。常用的属性有year, month, day;
  • datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond;
  • datetime.datetime:表示日期时间。
  • datetime.timedelta:表示时间间隔,即两个时间点之间的长度。
  • datetime.tzinfo:与时区有关的相关信息。

3.date类 
date类表示一个由年、月、日组成的日期。 
①类方法与类属性 
date类定义了一些常用的类方法与类属性,方便我们操作:

  • date.max、date.min:date对象所能表示的最大、最小日期;
  • date.resolution:date对象表示日期的最小单位。这里是天。
  • date.today():返回一个表示当前本地日期的date对象;
  • date.fromtimestamp(timestamp):根据给定的时间戮,返回一个date对象;
  • datetime.fromordinal(ordinal):将Gregorian日历时间转换为date对象;(Gregorian Calendar :一种日历表示方法,类似于我国的农历,西方国家使用比较多,此处不详细展开讨论。)

②实例方法和属性 
date提供的实例方法和属性:

  • date.year、date.month、date.day:年、月、日;
  • date.replace(year, month,day):生成一个新的日期对象,用参数指定的年,月,日代替原有对象中的属性。(原有对象仍保持不变)
  • date.timetuple():返回日期对应的time.struct_time对象;
  • date.toordinal():返回日期对应的Gregorian Calendar日期;
  • date.weekday():返回weekday,如果是星期一,返回0;如果是星期2,返回1,以此类推;
  • data.isoweekday():返回weekday,如果是星期一,返回1;如果是星期2,返回2,以此类推;
  • date.isocalendar():返回格式如(year,month,day)的元组;
  • date.isoformat():返回格式如’YYYY-MM-DD’的字符串;
  • date.strftime(fmt):自定义格式化字符串。在下面详细讲解。

③date支持的其他操作

  • date2 = date1 + timedelta #日期加上一个间隔,返回一个新的日期对象)
  • date2 = date1 - timedelta #日期隔去间隔,返回一个新的日期对象
  • timedelta = date1 - date2 #两个日期相减,返回一个时间间隔对象
  • date1 < date2 #两个日期进行比较

4.Time类 
time类表示时间,由时、分、秒以及微秒组成。 
①time类定义的类属性

  • time.min、time.max:time类所能表示的最小、最大时间。其中,time.min = time(0, 0, 0, 0),time.max = time(23, 59, 59, 999999);
  • time.resolution:时间的最小单位,这里是1微秒;

②time类提供的实例方法和属性

  • time.hour、time.minute、time.second、time.microsecond:时、分、秒、微秒;
  • time.tzinfo:时区信息;
  • time.replace([ hour[ , minute[ , second[ ,microsecond[ , tzinfo] ] ] ] ] ):创建一个新的时间对象,用参数指定的时、分、秒、微秒代替原有对象中的属性(原有对象仍保持不变);
  • time.isoformat():返回型如”HH:MM:SS”格式的字符串表示;
  • time.strftime(fmt):返回自定义格式化字符串。在下面详细介绍;

像date一样,也可以对两个time对象进行比较,或者相减返回一个时间间隔对象。

5.datetime类 
datetime是date与time的结合体,包括date与time的所有信息。 
①datetime类定义的类属性与方法

  • datetime.min、datetime.max:datetime所能表示的最小值与最大值;
  • datetime.resolution:datetime最小单位;
  • datetime.today():返回一个表示当前本地时间的datetime对象;
  • datetime.now([tz]):返回一个表示当前本地时间的datetime对象,如果提供了参数tz,则获取tz参数所指时区的本地时间;
  • datetime.utcnow():返回一个当前utc时间的datetime对象;
  • datetime.fromtimestamp(timestamp[,tz]):根据时间戮创建一个datetime对象,参数tz指定时区信息;
  • datetime.utcfromtimestamp(timestamp):根据时间戮创建一个datetime对象;
  • datetime.combine(date, time):根据date和time,创建一个datetime对象;
  • datetime.strptime(date_string, format):将格式字符串转换为datetime对象;

②datetime类提供的实例方法与属性

  • datetime.year、month、day、hour、minute、second、microsecond、tzinfo:年、月、日、时、分、秒、毫秒、时区;
  • datetime.date():获取date对象。
  • datetime.time():获取time对象。
  • datetime. replace([ year[ , month[ , day[ , hour[ , minute[ , second[ , microsecond[ ,tzinfo] ] ] ] ] ] ] ]): 返回一个替换了指定日期时间字段的新datetime对象。
  • datetime.timetuple ():返回一个时间元素, 等价于time.localtime()。
  • datetime. utctimetuple():返回UTC时间元组对象, 等价于time.localtime()。
  • datetime. toordinal ():返回日期对应的Gregorian Calendar 日期。
  • datetime. weekday ():返回 0 ~ 6 表示星期几(星期一是0,依此类推)。
  • datetime. isocalendar ():返回一个三元组格式 (year, month, day)。
  • datetime. isoformat ([ sep] ):返回一个 ISO 8601 格式的日期字符串,如 “YYYY-MM-DD”的字符串。
  • datetime. ctime():返回一个日期时间的C格式字符串,等效于time.ctime(time.mktime(dt.timetuple()));
  • datetime. strftime (format):返回自定义格式化字符串表示日期。

像date一样,也可以对两个datetime对象进行比较,或者相减返回一个时间间隔对象,或者日期时间加上一个间隔返回一个新的日期时间对象。 
6.timedelta与tzinfo类 
①timedelta类 
datetime.timedelta对象代表两个时间之间的的时间差,两个date或datetime对象相减时可以返回一个timedelta对象。 
timedelta包含数据days、seconds、microseconds,表示天数、秒数、微秒数,包含方法total_seconds() ,表示时间间隔的总秒数。 
②tzinfo类 
tzinfo:与时区有关的相关信息。

三、常见的日期时间操作 
1.将字符串格式的日期转换为时间戳 
方法:先将其转换为时间元组,再转换为时间戳。

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">>>> import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>
>>> thisTime = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2016-07-31 12:12:12"</span>
>>> timeTuple = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.strptime(thisTime, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%Y</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%m</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%d</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%H</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%M</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%S</span>"</span>)
>>> timeStamp = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.mktime(timeTuple))
>>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">print</span> timeStamp
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1469938332</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

2.将时间戳转换为指定格式的日期 
方法一

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">>>> import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>
>>> timeStamp = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1469938332</span>
>>> timeTuple = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">localtime</span>(timeStamp)
>>> otherTime = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.strftime(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%Y</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%m</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%d</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%H</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%M</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%S</span>"</span>, timeTuple)
>>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">print</span> otherTime
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2016</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">07</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">31</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

方法二

<code class="hljs python has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-prompt" style="color: rgb(0, 102, 102); box-sizing: border-box;">>>> </span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> datetime
<span class="hljs-prompt" style="color: rgb(0, 102, 102); box-sizing: border-box;">>>> </span>timeStamp = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1469938332</span>
<span class="hljs-prompt" style="color: rgb(0, 102, 102); box-sizing: border-box;">>>> </span>dt= datetime.datetime.utcfromtimestamp(timeStamp)
<span class="hljs-prompt" style="color: rgb(0, 102, 102); box-sizing: border-box;">>>> </span>otherTime = dt.strftime(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%Y-%m-%d %H:%M:%S"</span>)
<span class="hljs-prompt" style="color: rgb(0, 102, 102); box-sizing: border-box;">>>> </span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">print</span> otherTime
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2016</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">07</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">31</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">04</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

3.更改日期格式 
方法:先转换为时间元组,然后转换为其他格式。

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">>>> import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>
>>> thisTime = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2016-07-31 12:12:12"</span>
>>>timeTuple = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.strptime(thisTime, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%Y</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%m</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%d</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%H</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%M</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%S</span>"</span>)
>>>otherTime = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.strftime(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%Y</span>/<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%m</span>/<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%d</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%H</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%M</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%S</span>"</span>, timeTuple)
>>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">print</span> otherTime
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2016</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">07</span>/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">31</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

4.将当前时间并转换为指定日期格式 
方法一:

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">>>> import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>
>>> nowStamp = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>())  
>>> timeTuple = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">localtime</span>(nowStamp)
>>> otherTime = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.strftime(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%Y</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%m</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%d</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%H</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%M</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%S</span>"</span>, timeTuple)
>>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">print</span> otherTime
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2016</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">07</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">31</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">19</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">47</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

方法二:

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">>>> import datetime
>>> now = datetime.datetime.now()
>>> otherTime = now.strftime(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%Y</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%m</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%d</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%H</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%M</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%S</span>"</span>)
>>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">print</span> otherTime
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2016</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">07</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">31</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">45</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

5、获取今天、昨天和明天的日期

<code class="hljs python has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-prompt" style="color: rgb(0, 102, 102); box-sizing: border-box;">>>> </span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> datetime
<span class="hljs-prompt" style="color: rgb(0, 102, 102); box-sizing: border-box;">>>> </span>today = datetime.date.today()
<span class="hljs-prompt" style="color: rgb(0, 102, 102); box-sizing: border-box;">>>> </span>yesterday = today - datetime.timedelta(days=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)
<span class="hljs-prompt" style="color: rgb(0, 102, 102); box-sizing: border-box;">>>> </span>tomorrow = today + datetime.timedelta(days=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>) 
<span class="hljs-prompt" style="color: rgb(0, 102, 102); box-sizing: border-box;">>>> </span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">print</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"昨天:%s, 今天:%s, 明天:%s"</span> % (yesterday, today, tomorrow)
昨天:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2016</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">07</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>, 今天:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2016</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">07</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">31</span>, 明天:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2016</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">08</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">01</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

6.计算前1天、后一天的时间

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">>>> import datetime
>>> dtOneDayAgo = (datetime.datetime.now() - datetime.timedelta(days = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)).strftime(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%Y</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%m</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%d</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%H</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%M</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%S</span>"</span>)
>>> dtOneDayAfter = (datetime.datetime.now() + datetime.timedelta(days = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)).strftime(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%Y</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%m</span>-<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%d</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%H</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%M</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%S</span>"</span>)
>>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">print</span> dtOneDayAgo,dtOneDayAfter
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2016</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">07</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">39</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">54</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2016</span>-08-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">01</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">14</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

7、获取当前的时间 
(1)使用time模块

<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">>>> import <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>
>>> currentTime = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">time</span>.strftime(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%H</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%M</span>:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%S</span>"</span>)
>>> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">print</span> currentTime
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">44</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

(2)使用datetime模块

<code class="hljs vbscript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">>>> import datetime
>>> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">now</span> = datetime.datetime.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">now</span>()
>>> currentTime=str(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">now</span>.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">hour</span>)+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">":"</span>+str(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">now</span>.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">minute</span>)+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">":"</span>+str(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">now</span>.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">second</span>)
>>> print currentTime
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">52</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">18</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

四、日期和时间的格式化参数

  • %a 星期几的简写
  • %A 星期几的全称
  • %b 月分的简写
  • %B 月份的全称
  • %c 标准的日期的时间串
  • %C 年份的后两位数字
  • %d 十进制表示的每月的第几天
  • %D 月/天/年
  • %e 在两字符域中,十进制表示的每月的第几天
  • %F 年-月-日
  • %g 年份的后两位数字,使用基于周的年
  • %G 年分,使用基于周的年
  • %h 简写的月份名
  • %H 24小时制的小时
  • %I 12小时制的小时
  • %j 十进制表示的每年的第几天
  • %m 十进制表示的月份
  • %M 十时制表示的分钟数
  • %n 新行符
  • %p 本地的AM或PM的等价显示
  • %r 12小时的时间
  • %R 显示小时和分钟:hh:mm
  • %S 十进制的秒数
  • %t 水平制表符
  • %T 显示时分秒:hh:mm:ss
  • %u 每周的第几天,星期一为第一天 (值从0到6,星期一为0)
  • %U 第年的第几周,把星期日做为第一天(值从0到53)
  • %V 每年的第几周,使用基于周的年
  • %w 十进制表示的星期几(值从0到6,星期天为0)
  • %W 每年的第几周,把星期一做为第一天(值从0到53)
  • %x 标准的日期串
  • %X 标准的时间串
  • %y 不带世纪的十进制年份(值从0到99)
  • %Y 带世纪部分的十制年份
  • %z,%Z时区名称,如果不能得到时区名称则返回空字符。
  • %% 百分号

转载于:https://my.oschina.net/tangzhichao/blog/781534