{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# datetime 模块" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import datetime as dt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`datetime` 提供了基础时间和日期的处理。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## date 对象" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以使用 `date(year, month, day)` 产生一个 `date` 对象:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "d1 = dt.date(2007, 9, 25)\n", "d2 = dt.date(2008, 9, 25)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以格式化 `date` 对象的输出:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2007-09-25\n", "Tuesday, 09/25/07\n", "Tue, 09-25-2007\n" ] } ], "source": [ "print d1\n", "print d1.strftime('%A, %m/%d/%y')\n", "print d1.strftime('%a, %m-%d-%Y')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以看两个日期相差多久:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "366 days, 0:00:00\n" ] } ], "source": [ "print d2 - d1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "返回的是一个 `timedelta` 对象:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "366\n", "0\n" ] } ], "source": [ "d = d2 - d1\n", "print d.days\n", "print d.seconds" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "查看今天的日期:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2015-09-10\n" ] } ], "source": [ "print dt.date.today()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## time 对象" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以使用 `time(hour, min, sec, us)` 产生一个 `time` 对象:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "t1 = dt.time(15, 38)\n", "t2 = dt.time(18)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "改变显示格式:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15:38:00\n", "03:38, PM\n", "15:38:00, PM\n" ] } ], "source": [ "print t1\n", "print t1.strftime('%I:%M, %p')\n", "print t1.strftime('%H:%M:%S, %p')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "因为没有具体的日期信息,所以 `time` 对象不支持减法操作。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## datetime 对象" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以使用 `datetime(year, month, day, hr, min, sec, us)` 来创建一个 `datetime` 对象。 \n", "\n", "获得当前时间:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2015-09-10 20:58:50.148000\n" ] } ], "source": [ "d1 = dt.datetime.now()\n", "print d1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "给当前的时间加上 `30` 天,`timedelta` 的参数是 `timedelta(day, hr, min, sec, us)`:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2015-10-10 20:58:50.148000\n" ] } ], "source": [ "d2 = d1 + dt.timedelta(30)\n", "print d2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "除此之外,我们还可以通过一些指定格式的字符串来创建 `datetime` 对象:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2001-02-10 00:00:00\n" ] } ], "source": [ "print dt.datetime.strptime('2/10/01', '%m/%d/%y')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## datetime 格式字符表" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "字符|含义\n", "--|--\n", "`%a` | 星期英文缩写\n", "`%A` | 星期英文\n", "`%w` | 一星期的第几天,`[0(sun),6]`\n", "`%b` | 月份英文缩写\n", "`%B` | 月份英文\n", "`%d` | 日期,`[01,31]`\n", "`%H` | 小时,`[00,23]`\n", "`%I` | 小时,`[01,12]`\n", "`%j` | 一年的第几天,`[001,366]`\n", "`%m` | 月份,`[01,12]`\n", "`%M` | 分钟,`[00,59]`\n", "`%p` | AM 和 PM\n", "`%S` | 秒钟,`[00,61]` (大概是有闰秒的存在)\n", "`%U` | 一年中的第几个星期,星期日为第一天,`[00,53]`\n", "`%W` | 一年中的第几个星期,星期一为第一天,`[00,53]`\n", "`%y` | 没有世纪的年份\n", "`%Y` | 完整的年份" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }