{"id":360,"date":"2020-07-14T03:50:45","date_gmt":"2020-07-14T03:50:45","guid":{"rendered":"http:\/\/feellikelearning.com\/?p=360"},"modified":"2021-01-26T01:22:53","modified_gmt":"2021-01-26T01:22:53","slug":"cn-how-to-manipulate-time-variables-in-python","status":"publish","type":"post","link":"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/","title":{"rendered":"Python\u65f6\u95f4\u53d8\u91cf\u7b14\u8bb0 | How to manipulate time variables in python"},"content":{"rendered":"\n<h2>Example: x days later<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import datetime as dt\ntoday = dt.datetime.today()\none_month_later = today + dt.timedelta(days=30)\nprint(f\"today: {today}\")\nprint(f\"one_month_later: {one_month_later}\")<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>2020-07-13 20:52:21.473270\none_month_later: 2020-08-12 20:52:21.473270<\/code><\/pre>\n\n\n\n<h2>Example: datetime to string<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">today = dt.datetime.today()\nprint(today.strftime('%Y%m%d')\nprint(today.strftime('%Y-%m-%d')<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>20210125\n2021-01-25<\/code><\/pre>\n\n\n\n<h2>Example: input date\/time from string: strptime (string parsed to time)<\/h2>\n\n\n\n<p>%y (lower case) year in 2 digits, like 20<br>%Y (capital case) year in 4 digits, like 2020<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; dt.datetime.strptime('2020-01-01', '%Y-%m-%d')\ndatetime.datetime(2020, 1, 1, 0, 0)\n\n&gt;&gt;&gt; dt.datetime.strptime('20-01-01', '%y-%m-%d')\ndatetime.datetime(2020, 1, 1, 0, 0)<\/code><\/pre>\n\n\n\n<p>Example: number of days between two dates<\/p>\n\n\n\n<p>\u4e24\u4e2adatetime.datetime type\u53d8\u91cf\u76f8\u51cf\u5f97\u5230datetime.timedelta<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; today - one_month_later\ndatetime.timedelta(days=-30)\n\n&gt;&gt;&gt; print(today - one_month_later)\n-30 days, 0:00:00\n\n&gt;&gt;&gt; d = today - one_month_later\n&gt;&gt;&gt; d.days\n-30<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Example: x days later Example: datetime to string Example: input date\/time from string: strptime (string parsed to time) %y (lower case) year in 2 digits, like 20%Y (capital case) year in 4 digits, like 2020 Example: number of days between&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.10 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python\u65f6\u95f4\u53d8\u91cf\u7b14\u8bb0 | How to manipulate time variables in python | Feel Like Learning<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python\u65f6\u95f4\u53d8\u91cf\u7b14\u8bb0 | How to manipulate time variables in python | Feel Like Learning\" \/>\n<meta property=\"og:description\" content=\"Example: x days later Example: datetime to string Example: input date\/time from string: strptime (string parsed to time) %y (lower case) year in 2 digits, like 20%Y (capital case) year in 4 digits, like 2020 Example: number of days between...Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant\" \/>\n<meta property=\"og:site_name\" content=\"Feel Like Learning\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-14T03:50:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-01-26T01:22:53+00:00\" \/>\n<meta name=\"author\" content=\"feellikelearning\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"feellikelearning\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant#article\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant\"},\"author\":{\"name\":\"feellikelearning\",\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"headline\":\"Python\u65f6\u95f4\u53d8\u91cf\u7b14\u8bb0 | How to manipulate time variables in python\",\"datePublished\":\"2020-07-14T03:50:45+00:00\",\"dateModified\":\"2021-01-26T01:22:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant\"},\"wordCount\":53,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant\",\"url\":\"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant\",\"name\":\"Python\u65f6\u95f4\u53d8\u91cf\u7b14\u8bb0 | How to manipulate time variables in python | Feel Like Learning\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/#website\"},\"datePublished\":\"2020-07-14T03:50:45+00:00\",\"dateModified\":\"2021-01-26T01:22:53+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/feellikelearning.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\u65f6\u95f4\u53d8\u91cf\u7b14\u8bb0 | How to manipulate time variables in python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/feellikelearning.com\/#website\",\"url\":\"https:\/\/feellikelearning.com\/\",\"name\":\"Feel Like Learning\",\"description\":\"\u7a0b\u5e8f\uff5c\u751f\u6d3b\uff5c\u5b66\u5230\u5c31\u662f\u8d5a\u5230\",\"publisher\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/feellikelearning.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\",\"name\":\"feellikelearning\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g\",\"caption\":\"feellikelearning\"},\"logo\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/feellikelearning.com\/index.php\/author\/feellikelearning\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python\u65f6\u95f4\u53d8\u91cf\u7b14\u8bb0 | How to manipulate time variables in python | Feel Like Learning","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant","og_locale":"en_US","og_type":"article","og_title":"Python\u65f6\u95f4\u53d8\u91cf\u7b14\u8bb0 | How to manipulate time variables in python | Feel Like Learning","og_description":"Example: x days later Example: datetime to string Example: input date\/time from string: strptime (string parsed to time) %y (lower case) year in 2 digits, like 20%Y (capital case) year in 4 digits, like 2020 Example: number of days between...Read more","og_url":"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant","og_site_name":"Feel Like Learning","article_published_time":"2020-07-14T03:50:45+00:00","article_modified_time":"2021-01-26T01:22:53+00:00","author":"feellikelearning","twitter_card":"summary_large_image","twitter_misc":{"Written by":"feellikelearning","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant#article","isPartOf":{"@id":"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant"},"author":{"name":"feellikelearning","@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"headline":"Python\u65f6\u95f4\u53d8\u91cf\u7b14\u8bb0 | How to manipulate time variables in python","datePublished":"2020-07-14T03:50:45+00:00","dateModified":"2021-01-26T01:22:53+00:00","mainEntityOfPage":{"@id":"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant"},"wordCount":53,"commentCount":0,"publisher":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant#respond"]}]},{"@type":"WebPage","@id":"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant","url":"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant","name":"Python\u65f6\u95f4\u53d8\u91cf\u7b14\u8bb0 | How to manipulate time variables in python | Feel Like Learning","isPartOf":{"@id":"https:\/\/feellikelearning.com\/#website"},"datePublished":"2020-07-14T03:50:45+00:00","dateModified":"2021-01-26T01:22:53+00:00","breadcrumb":{"@id":"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/feellikelearning.com\/index.php\/2020\/07\/14\/cn-how-to-manipulate-time-variables-in-python\/?variant=zh-hant#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/feellikelearning.com\/"},{"@type":"ListItem","position":2,"name":"Python\u65f6\u95f4\u53d8\u91cf\u7b14\u8bb0 | How to manipulate time variables in python"}]},{"@type":"WebSite","@id":"https:\/\/feellikelearning.com\/#website","url":"https:\/\/feellikelearning.com\/","name":"Feel Like Learning","description":"\u7a0b\u5e8f\uff5c\u751f\u6d3b\uff5c\u5b66\u5230\u5c31\u662f\u8d5a\u5230","publisher":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/feellikelearning.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a","name":"feellikelearning","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g","caption":"feellikelearning"},"logo":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/"},"url":"https:\/\/feellikelearning.com\/index.php\/author\/feellikelearning\/"}]}},"_links":{"self":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/360"}],"collection":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/comments?post=360"}],"version-history":[{"count":6,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/360\/revisions"}],"predecessor-version":[{"id":1192,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/360\/revisions\/1192"}],"wp:attachment":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/media?parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/categories?post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/tags?post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}