{"id":78601,"date":"2024-02-27T01:39:05","date_gmt":"2024-02-26T16:39:05","guid":{"rendered":"https://freelance.indieverse.co.jp/media/?p=78601"},"modified":"2025-11-01T16:37:05","modified_gmt":"2025-11-01T07:37:05","slug":"python-datetime","status":"publish","type":"post","link":"https://freelance.indieverse.co.jp/media/programming/python/python-datetime","title":{"rendered":"pythonのdatetimeで日付だけ取得する方法について現役エンジニアが解説"},"content":{"rendered":"<ul>\n<li>datetimeで本日の日付のみ取得するにはどうしたらいいんだろう</li>\n</ul>\n<p>と悩まれている方向けに、この記事ではdatetimeの日付だけ取得する方法について解説します。</p>\n<h2>\ndatetimeモジュールとは</h2>\n<p>Pythonのdatetimeモジュールは、日付や時間を簡単に扱うためのクラスを提供します。</p>\n<p>このモジュールには、日付（date）、時間（time）、日付と時間の組み合わせ（datetime）、時間差（timedelta）などを扱うためのクラスが含まれています。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from datetime import date\r\n# 今日の日付を取得\r\ntoday = date.today()\r\nprint(today)</code></pre>\n</div>\n<p>このコードの実行結果は、実行した日によって異なりますが、`YYYY-MM-DD`形式の日付が出力されます。</p>\n<h2>日付の取得方法</h2>\n<p>日付を取得する基本的な方法には、現在の日付を取得する方法と、特定の日付を指定して生成する方法があります。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from datetime import date\r\n# 今日の日付を取得\r\ntoday = date.today()\r\nprint(\"今日の日付:\", today)\r\n# 特定の日付を指定して生成\r\nsome_day = date(2023, 4, 1)\r\nprint(\"特定の日付:\", some_day)</code></pre>\n</div>\n<h2>日付のフォーマット</h2>\n<p>日付を人が読みやすい形式にフォーマットするには、strftimeメソッドを使用します。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from datetime import date\r\ntoday = date.today()\r\n# YYYY-MM-DD 形式で出力\r\nprint(\"フォーマット後の日付:\", today.strftime(\"%Y-%m-%d\"))</code></pre>\n</div>\n","protected":false},"excerpt":{"rendered":"<p>Pythonのdatetimeで日付だけを取得する方法を現役エンジニアが解説。date.today()や指定日生成、strftimeでのフォーマットまでサンプル付きで紹介します。</p>\n","protected":false},"author":105,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[191],"tags":[6],"class_list":["post-78601","post","type-post","status-publish","format-standard","hentry","category-python","tag-python"],"aioseo_notices":[],"meta_description":"Pythonのdatetimeで日付だけを取得する方法を現役エンジニアが解説。date.today()や指定日生成、strftimeでのフォーマットまでサンプル付きで紹介します。","_links":{"self":[{"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/posts/78601","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/posts"}],"about":[{"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/types/post"}],"author":[{"embeddable":true,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/users/105"}],"replies":[{"embeddable":true,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/comments?post=78601"}],"version-history":[{"count":3,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/posts/78601/revisions"}],"predecessor-version":[{"id":93522,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/posts/78601/revisions/93522"}],"wp:attachment":[{"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/media?parent=78601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/categories?post=78601"},{"taxonomy":"post_tag","embeddable":true,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/tags?post=78601"}],"curies":[{"name":"wp","href":"https://api.w.org/{rel}","templated":true}]}}