{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c050689b",
   "metadata": {},
   "source": [
    "# 模块datetime:日期时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0053c459",
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime as dt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9a9f6c3",
   "metadata": {},
   "source": [
    "## 日期date对象"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "0bb585c3",
   "metadata": {},
   "source": [
    "产生一个有年月日信息的date对象:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "106ccc5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "d1 = dt.date(2007, 9, 25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "19feda31",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2007, 9, 25)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c1b10bdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "d2 = dt.date(2008, 9, 25)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "21af4e09",
   "metadata": {},
   "source": [
    "分别表示2008年9月25日和2007年9月25日。日期可以打印出来:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2c2fdf1b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2007-09-25\n"
     ]
    }
   ],
   "source": [
    "print(d1)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "2d464813",
   "metadata": {},
   "source": [
    "还可以调用date对象的`.strftime()`方法将日期转化为特定格式:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "451adeb3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Tuesday, 09/25/07'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d1.strftime('%A, %m/%d/%y')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f2fefa1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Tue, 09-25-2007'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d1.strftime('%a, %m-%d-%Y')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "82b172ba",
   "metadata": {},
   "source": [
    "## 时间间隔timedelta对象\n",
    "\n",
    "两个日期可以相减:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ae670df1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.timedelta(days=366)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d2 - d1"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e7f004af",
   "metadata": {},
   "source": [
    "## 时间time对象\n",
    "\n",
    "产生两个时间对象:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1adbc112",
   "metadata": {},
   "outputs": [],
   "source": [
    "t1 = dt.time(15, 38)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "53e0a156",
   "metadata": {},
   "outputs": [],
   "source": [
    "t2 = dt.time(18)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "940efbcb",
   "metadata": {},
   "source": [
    "分别表示15点38分和18点整:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a3ee04e3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15:38:00\n"
     ]
    }
   ],
   "source": [
    "print(t1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ea5b9e8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.time(18, 0)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t2"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c491dfcd",
   "metadata": {},
   "source": [
    "输出格式同样可以用`.strftime()`方法改变:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "be2d1c77",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'15:38:00, PM'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1.strftime('%H:%M:%S, %p')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29fc6da4",
   "metadata": {},
   "source": [
    "## 日期时间datetime对象"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b75a312",
   "metadata": {},
   "source": [
    "同时带日期与时间的对象:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e7d1a89b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2022, 3, 7, 2, 49, 39)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt.datetime(2022, 3, 7, 2, 49, 39)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5035b281",
   "metadata": {},
   "outputs": [],
   "source": [
    "d1 = dt.datetime.now()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5214063e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-05-02 21:05:26.567396\n"
     ]
    }
   ],
   "source": [
    "print(d1)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "0f70d307",
   "metadata": {},
   "source": [
    "date和datetime对象都支持减法;此外,它们还支持与timedelta对象的加法。例如,将当前时间加上30天:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2abb97bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022-06-01 21:05:26.567396\n"
     ]
    }
   ],
   "source": [
    "print(d1 + dt.timedelta(30))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "66c0dc07",
   "metadata": {},
   "source": [
    "## 日期格式\n",
    "\n",
    "字符\t| 含义\n",
    "-- | --\n",
    "%a\t | 星期英文缩写\n",
    "%A\t | 星期英文\n",
    "%w\t | 一星期的第几天,[0(sun),6]\n",
    "%b\t | 月份英文缩写\n",
    "%B\t | 月份英文\n",
    "%d\t | 日期,[01,31]\n",
    "%H\t | 小时,[00,23]\n",
    "%I\t | 小时,[01,12]\n",
    "%j\t | 一年的第几天,[001,366]\n",
    "%m\t | 月份,[01,12]\n",
    "%M\t | 分钟,[00,59]\n",
    "%p\t | AM 和PM\n",
    "%S\t | 秒钟,[00,61] \n",
    "%U\t | 一年中的第几个星期,星期日为第一天,[00,53]\n",
    "%W\t | 一年中的第几个星期,星期一为第一天,[00,53]\n",
    "%y\t | 没有世纪的年份\n",
    "%Y\t | 完整的年份\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e429711",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}