[{"data":1,"prerenderedAt":1708},["ShallowReactive",2],{"doc-\u002Fexamples\u002Fpython-working-with-dates-example":3},{"id":4,"title":5,"body":6,"description":1701,"extension":1702,"meta":1703,"navigation":92,"path":1704,"seo":1705,"stem":1706,"__hash__":1707},"content\u002Fexamples\u002Fpython-working-with-dates-example.md","Python Working with Dates Example",{"type":7,"value":8,"toc":1679},"minimark",[9,13,22,25,47,52,190,201,204,208,225,229,232,253,257,263,299,307,311,323,366,369,394,401,405,410,454,456,490,493,516,523,527,532,619,621,656,659,696,702,709,713,719,821,824,863,866,886,897,904,908,921,1060,1062,1092,1095,1104,1108,1114,1231,1233,1266,1272,1275,1385,1387,1391,1394,1425,1428,1452,1455,1533,1536,1547,1551,1554,1568,1571,1591,1595,1600,1608,1612,1623,1627,1632,1636,1642,1646,1649,1653,1675],[10,11,5],"h1",{"id":12},"python-working-with-dates-example",[14,15,16,17,21],"p",{},"Python can work with dates and times using the ",[18,19,20],"code",{},"datetime"," module. In this example, you will learn how to do common date tasks with small, clear examples.",[14,23,24],{},"You will see how to:",[26,27,28,32,35,38,41,44],"ul",{},[29,30,31],"li",{},"Get the current date",[29,33,34],{},"Get the current date and time",[29,36,37],{},"Format dates as readable text",[29,39,40],{},"Convert a string into a date",[29,42,43],{},"Compare dates",[29,45,46],{},"Find the number of days between dates",[48,49,51],"h2",{"id":50},"quick-example","Quick example",[53,54,59],"pre",{"className":55,"code":56,"language":57,"meta":58,"style":58},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","from datetime import datetime, date\n\ntoday = date.today()\nprint(today)\n\nnow = datetime.now()\nprint(now.strftime(\"%Y-%m-%d %H:%M:%S\"))\n","python","",[18,60,61,87,94,117,132,137,154],{"__ignoreMap":58},[62,63,66,70,74,77,80,84],"span",{"class":64,"line":65},"line",1,[62,67,69],{"class":68},"sVHd0","from",[62,71,73],{"class":72},"su5hD"," datetime ",[62,75,76],{"class":68},"import",[62,78,79],{"class":72}," datetime",[62,81,83],{"class":82},"sP7_E",",",[62,85,86],{"class":72}," date\n",[62,88,90],{"class":64,"line":89},2,[62,91,93],{"emptyLinePlaceholder":92},true,"\n",[62,95,97,100,104,107,110,114],{"class":64,"line":96},3,[62,98,99],{"class":72},"today ",[62,101,103],{"class":102},"smGrS","=",[62,105,106],{"class":72}," date",[62,108,109],{"class":82},".",[62,111,113],{"class":112},"slqww","today",[62,115,116],{"class":82},"()\n",[62,118,120,124,127,129],{"class":64,"line":119},4,[62,121,123],{"class":122},"sptTA","print",[62,125,126],{"class":82},"(",[62,128,113],{"class":112},[62,130,131],{"class":82},")\n",[62,133,135],{"class":64,"line":134},5,[62,136,93],{"emptyLinePlaceholder":92},[62,138,140,143,145,147,149,152],{"class":64,"line":139},6,[62,141,142],{"class":72},"now ",[62,144,103],{"class":102},[62,146,79],{"class":72},[62,148,109],{"class":82},[62,150,151],{"class":112},"now",[62,153,116],{"class":82},[62,155,157,159,161,163,165,168,170,174,178,182,185,187],{"class":64,"line":156},7,[62,158,123],{"class":122},[62,160,126],{"class":82},[62,162,151],{"class":112},[62,164,109],{"class":82},[62,166,167],{"class":112},"strftime",[62,169,126],{"class":82},[62,171,173],{"class":172},"sjJ54","\"",[62,175,177],{"class":176},"s_sjI","%Y-%m-",[62,179,181],{"class":180},"srdBf","%d",[62,183,184],{"class":176}," %H:%M:%S",[62,186,173],{"class":172},[62,188,189],{"class":82},"))\n",[14,191,192,193,196,197,200],{},"Use ",[18,194,195],{},"date.today()"," for the current date and ",[18,198,199],{},"datetime.now()"," when you also need the current time.",[202,203],"hr",{},[48,205,207],{"id":206},"what-this-example-covers","What this example covers",[26,209,210,213,216,219,222],{},[29,211,212],{},"Show how to get the current date",[29,214,215],{},"Show how to get the current date and time",[29,217,218],{},"Show how to format dates as readable strings",[29,220,221],{},"Show how to convert text into a date",[29,223,224],{},"Show how to compare two dates",[48,226,228],{"id":227},"when-to-use-date-handling-in-python","When to use date handling in Python",[14,230,231],{},"Date handling is useful when you need to:",[26,233,234,237,240,243,246],{},[29,235,236],{},"Display today's date in a program",[29,238,239],{},"Timestamp files or logs",[29,241,242],{},"Check deadlines or due dates",[29,244,245],{},"Calculate how many days are between two dates",[29,247,248,249,252],{},"Convert user input like ",[18,250,251],{},"2025-04-22"," into a date object",[48,254,256],{"id":255},"main-tools-used-in-this-example","Main tools used in this example",[14,258,259,260,262],{},"This page uses a few important parts of the ",[18,261,20],{}," module:",[26,264,265,271,277,282,287,293],{},[29,266,267,270],{},[18,268,269],{},"datetime.date"," for dates only",[29,272,273,276],{},[18,274,275],{},"datetime.datetime"," for date and time together",[29,278,279,281],{},[18,280,199],{}," to get the current date and time",[29,283,284,286],{},[18,285,195],{}," to get the current date",[29,288,289,292],{},[18,290,291],{},"strftime()"," to format a date as text",[29,294,295,298],{},[18,296,297],{},"strptime()"," to parse text into a date",[14,300,301,302,109],{},"If you want a broader overview, see the ",[303,304,306],"a",{"href":305},"\u002Fstandard-library\u002Fpython-datetime-module-overview\u002F","Python datetime module overview",[48,308,310],{"id":309},"step-1-get-the-current-date","Step 1: Get the current date",[14,312,313,314,317,318,320,321,109],{},"Import ",[18,315,316],{},"date"," from ",[18,319,20],{},", then call ",[18,322,195],{},[53,324,326],{"className":55,"code":325,"language":57,"meta":58,"style":58},"from datetime import date\n\ntoday = date.today()\nprint(today)\n",[18,327,328,338,342,356],{"__ignoreMap":58},[62,329,330,332,334,336],{"class":64,"line":65},[62,331,69],{"class":68},[62,333,73],{"class":72},[62,335,76],{"class":68},[62,337,86],{"class":72},[62,339,340],{"class":64,"line":89},[62,341,93],{"emptyLinePlaceholder":92},[62,343,344,346,348,350,352,354],{"class":64,"line":96},[62,345,99],{"class":72},[62,347,103],{"class":102},[62,349,106],{"class":72},[62,351,109],{"class":82},[62,353,113],{"class":112},[62,355,116],{"class":82},[62,357,358,360,362,364],{"class":64,"line":119},[62,359,123],{"class":122},[62,361,126],{"class":82},[62,363,113],{"class":112},[62,365,131],{"class":82},[14,367,368],{},"Example output:",[53,370,372],{"className":55,"code":371,"language":57,"meta":58,"style":58},"2026-04-22\n",[18,373,374],{"__ignoreMap":58},[62,375,376,379,382,385,389,391],{"class":64,"line":65},[62,377,378],{"class":180},"2026",[62,380,381],{"class":102},"-",[62,383,384],{"class":180},"0",[62,386,388],{"class":387},"sx12J","4",[62,390,381],{"class":102},[62,392,393],{"class":180},"22\n",[14,395,396,397,400],{},"By default, Python prints dates in ",[18,398,399],{},"year-month-day"," format.",[48,402,404],{"id":403},"step-2-get-the-current-date-and-time","Step 2: Get the current date and time",[14,406,407,408,109],{},"If you also need the time, use ",[18,409,199],{},[53,411,413],{"className":55,"code":412,"language":57,"meta":58,"style":58},"from datetime import datetime\n\nnow = datetime.now()\nprint(now)\n",[18,414,415,426,430,444],{"__ignoreMap":58},[62,416,417,419,421,423],{"class":64,"line":65},[62,418,69],{"class":68},[62,420,73],{"class":72},[62,422,76],{"class":68},[62,424,425],{"class":72}," datetime\n",[62,427,428],{"class":64,"line":89},[62,429,93],{"emptyLinePlaceholder":92},[62,431,432,434,436,438,440,442],{"class":64,"line":96},[62,433,142],{"class":72},[62,435,103],{"class":102},[62,437,79],{"class":72},[62,439,109],{"class":82},[62,441,151],{"class":112},[62,443,116],{"class":82},[62,445,446,448,450,452],{"class":64,"line":119},[62,447,123],{"class":122},[62,449,126],{"class":82},[62,451,151],{"class":112},[62,453,131],{"class":82},[14,455,368],{},[53,457,459],{"className":55,"code":458,"language":57,"meta":58,"style":58},"2026-04-22 14:35:10.123456\n",[18,460,461],{"__ignoreMap":58},[62,462,463,465,467,469,471,473,476,479,482,485,487],{"class":64,"line":65},[62,464,378],{"class":180},[62,466,381],{"class":102},[62,468,384],{"class":180},[62,470,388],{"class":387},[62,472,381],{"class":102},[62,474,475],{"class":180},"22",[62,477,478],{"class":180}," 14",[62,480,481],{"class":82},":",[62,483,484],{"class":180},"35",[62,486,481],{"class":82},[62,488,489],{"class":180},"10.123456\n",[14,491,492],{},"This includes:",[26,494,495,498,501,504,507,510,513],{},[29,496,497],{},"Year",[29,499,500],{},"Month",[29,502,503],{},"Day",[29,505,506],{},"Hour",[29,508,509],{},"Minute",[29,511,512],{},"Second",[29,514,515],{},"Microseconds",[14,517,518,519,109],{},"For a focused explanation, see ",[303,520,522],{"href":521},"\u002Fstandard-library\u002Fdatetime.now-explained","datetime.now() explained",[48,524,526],{"id":525},"step-3-format-a-date-for-display","Step 3: Format a date for display",[14,528,192,529,531],{},[18,530,291],{}," to turn a date or datetime object into a string in the format you want.",[53,533,535],{"className":55,"code":534,"language":57,"meta":58,"style":58},"from datetime import date\n\ntoday = date.today()\n\nprint(today.strftime(\"%d-%m-%Y\"))\nprint(today.strftime(\"%Y\u002F%m\u002F%d\"))\n",[18,536,537,547,551,565,569,594],{"__ignoreMap":58},[62,538,539,541,543,545],{"class":64,"line":65},[62,540,69],{"class":68},[62,542,73],{"class":72},[62,544,76],{"class":68},[62,546,86],{"class":72},[62,548,549],{"class":64,"line":89},[62,550,93],{"emptyLinePlaceholder":92},[62,552,553,555,557,559,561,563],{"class":64,"line":96},[62,554,99],{"class":72},[62,556,103],{"class":102},[62,558,106],{"class":72},[62,560,109],{"class":82},[62,562,113],{"class":112},[62,564,116],{"class":82},[62,566,567],{"class":64,"line":119},[62,568,93],{"emptyLinePlaceholder":92},[62,570,571,573,575,577,579,581,583,585,587,590,592],{"class":64,"line":134},[62,572,123],{"class":122},[62,574,126],{"class":82},[62,576,113],{"class":112},[62,578,109],{"class":82},[62,580,167],{"class":112},[62,582,126],{"class":82},[62,584,173],{"class":172},[62,586,181],{"class":180},[62,588,589],{"class":176},"-%m-%Y",[62,591,173],{"class":172},[62,593,189],{"class":82},[62,595,596,598,600,602,604,606,608,610,613,615,617],{"class":64,"line":139},[62,597,123],{"class":122},[62,599,126],{"class":82},[62,601,113],{"class":112},[62,603,109],{"class":82},[62,605,167],{"class":112},[62,607,126],{"class":82},[62,609,173],{"class":172},[62,611,612],{"class":176},"%Y\u002F%m\u002F",[62,614,181],{"class":180},[62,616,173],{"class":172},[62,618,189],{"class":82},[14,620,368],{},[53,622,624],{"className":55,"code":623,"language":57,"meta":58,"style":58},"22-04-2026\n2026\u002F04\u002F22\n",[18,625,626,641],{"__ignoreMap":58},[62,627,628,630,632,634,636,638],{"class":64,"line":65},[62,629,475],{"class":180},[62,631,381],{"class":102},[62,633,384],{"class":180},[62,635,388],{"class":387},[62,637,381],{"class":102},[62,639,640],{"class":180},"2026\n",[62,642,643,645,648,650,652,654],{"class":64,"line":89},[62,644,378],{"class":180},[62,646,647],{"class":102},"\u002F",[62,649,384],{"class":180},[62,651,388],{"class":387},[62,653,647],{"class":102},[62,655,393],{"class":180},[14,657,658],{},"Common format codes:",[26,660,661,667,673,678,684,690],{},[29,662,663,666],{},[18,664,665],{},"%Y"," = 4-digit year",[29,668,669,672],{},[18,670,671],{},"%m"," = month as a number",[29,674,675,677],{},[18,676,181],{}," = day of month",[29,679,680,683],{},[18,681,682],{},"%H"," = hour",[29,685,686,689],{},[18,687,688],{},"%M"," = minute",[29,691,692,695],{},[18,693,694],{},"%S"," = second",[14,697,698,699,701],{},"Important: ",[18,700,291],{}," changes how the value is displayed. It does not change the original date object.",[14,703,704,705,109],{},"You can learn more in ",[303,706,708],{"href":707},"\u002Fstandard-library\u002Fdatetime.strftime-explained","datetime.strftime() explained",[48,710,712],{"id":711},"step-4-convert-a-string-into-a-date","Step 4: Convert a string into a date",[14,714,192,715,718],{},[18,716,717],{},"datetime.strptime()"," when you have text and want to turn it into a date value.",[53,720,722],{"className":55,"code":721,"language":57,"meta":58,"style":58},"from datetime import datetime\n\ntext = \"2026-04-22\"\nparsed_date = datetime.strptime(text, \"%Y-%m-%d\").date()\n\nprint(parsed_date)\nprint(type(parsed_date))\n",[18,723,724,734,738,754,790,794,805],{"__ignoreMap":58},[62,725,726,728,730,732],{"class":64,"line":65},[62,727,69],{"class":68},[62,729,73],{"class":72},[62,731,76],{"class":68},[62,733,425],{"class":72},[62,735,736],{"class":64,"line":89},[62,737,93],{"emptyLinePlaceholder":92},[62,739,740,743,745,748,751],{"class":64,"line":96},[62,741,742],{"class":72},"text ",[62,744,103],{"class":102},[62,746,747],{"class":172}," \"",[62,749,750],{"class":176},"2026-04-22",[62,752,753],{"class":172},"\"\n",[62,755,756,759,761,763,765,768,770,773,775,777,779,781,783,786,788],{"class":64,"line":119},[62,757,758],{"class":72},"parsed_date ",[62,760,103],{"class":102},[62,762,79],{"class":72},[62,764,109],{"class":82},[62,766,767],{"class":112},"strptime",[62,769,126],{"class":82},[62,771,772],{"class":112},"text",[62,774,83],{"class":82},[62,776,747],{"class":172},[62,778,177],{"class":176},[62,780,181],{"class":180},[62,782,173],{"class":172},[62,784,785],{"class":82},").",[62,787,316],{"class":112},[62,789,116],{"class":82},[62,791,792],{"class":64,"line":134},[62,793,93],{"emptyLinePlaceholder":92},[62,795,796,798,800,803],{"class":64,"line":139},[62,797,123],{"class":122},[62,799,126],{"class":82},[62,801,802],{"class":112},"parsed_date",[62,804,131],{"class":82},[62,806,807,809,811,815,817,819],{"class":64,"line":156},[62,808,123],{"class":122},[62,810,126],{"class":82},[62,812,814],{"class":813},"sZMiF","type",[62,816,126],{"class":82},[62,818,802],{"class":112},[62,820,189],{"class":82},[14,822,823],{},"Output:",[53,825,827],{"className":55,"code":826,"language":57,"meta":58,"style":58},"2026-04-22\n\u003Cclass 'datetime.date'>\n",[18,828,829,843],{"__ignoreMap":58},[62,830,831,833,835,837,839,841],{"class":64,"line":65},[62,832,378],{"class":180},[62,834,381],{"class":102},[62,836,384],{"class":180},[62,838,388],{"class":387},[62,840,381],{"class":102},[62,842,393],{"class":180},[62,844,845,848,852,855,857,860],{"class":64,"line":89},[62,846,847],{"class":102},"\u003C",[62,849,851],{"class":850},"sbsja","class",[62,853,854],{"class":172}," '",[62,856,269],{"class":176},[62,858,859],{"class":172},"'",[62,861,862],{"class":102},">\n",[14,864,865],{},"The format string must match the input text exactly:",[26,867,868,877],{},[29,869,870,873,874],{},[18,871,872],{},"\"2026-04-22\""," matches ",[18,875,876],{},"\"%Y-%m-%d\"",[29,878,879,882,883],{},[18,880,881],{},"\"22\u002F04\u002F2026\""," would need ",[18,884,885],{},"\"%d\u002F%m\u002F%Y\"",[14,887,888,889,892,893,109],{},"If the format does not match, Python raises a ",[18,890,891],{},"ValueError",". If that happens, see ",[303,894,896],{"href":895},"\u002Ferrors\u002Fvalueerror-in-python-causes-and-fixes\u002F","ValueError in Python: causes and fixes",[14,898,899,900,109],{},"You can also read ",[303,901,903],{"href":902},"\u002Fstandard-library\u002Fdatetime.strptime-explained","datetime.strptime() explained",[48,905,907],{"id":906},"step-5-compare-dates","Step 5: Compare dates",[14,909,910,911,913,914,917,918,109],{},"You can compare date objects with ",[18,912,847],{},", ",[18,915,916],{},">",", and ",[18,919,920],{},"==",[53,922,924],{"className":55,"code":923,"language":57,"meta":58,"style":58},"from datetime import date\n\nstart_date = date(2026, 4, 20)\ndeadline = date(2026, 4, 25)\n\nprint(start_date \u003C deadline)\nprint(start_date == deadline)\n\nif start_date \u003C deadline:\n    print(\"The start date is earlier than the deadline.\")\n",[18,925,926,936,940,965,989,993,1008,1022,1027,1043],{"__ignoreMap":58},[62,927,928,930,932,934],{"class":64,"line":65},[62,929,69],{"class":68},[62,931,73],{"class":72},[62,933,76],{"class":68},[62,935,86],{"class":72},[62,937,938],{"class":64,"line":89},[62,939,93],{"emptyLinePlaceholder":92},[62,941,942,945,947,949,951,953,955,958,960,963],{"class":64,"line":96},[62,943,944],{"class":72},"start_date ",[62,946,103],{"class":102},[62,948,106],{"class":112},[62,950,126],{"class":82},[62,952,378],{"class":180},[62,954,83],{"class":82},[62,956,957],{"class":180}," 4",[62,959,83],{"class":82},[62,961,962],{"class":180}," 20",[62,964,131],{"class":82},[62,966,967,970,972,974,976,978,980,982,984,987],{"class":64,"line":119},[62,968,969],{"class":72},"deadline ",[62,971,103],{"class":102},[62,973,106],{"class":112},[62,975,126],{"class":82},[62,977,378],{"class":180},[62,979,83],{"class":82},[62,981,957],{"class":180},[62,983,83],{"class":82},[62,985,986],{"class":180}," 25",[62,988,131],{"class":82},[62,990,991],{"class":64,"line":134},[62,992,93],{"emptyLinePlaceholder":92},[62,994,995,997,999,1001,1003,1006],{"class":64,"line":139},[62,996,123],{"class":122},[62,998,126],{"class":82},[62,1000,944],{"class":112},[62,1002,847],{"class":102},[62,1004,1005],{"class":112}," deadline",[62,1007,131],{"class":82},[62,1009,1010,1012,1014,1016,1018,1020],{"class":64,"line":156},[62,1011,123],{"class":122},[62,1013,126],{"class":82},[62,1015,944],{"class":112},[62,1017,920],{"class":102},[62,1019,1005],{"class":112},[62,1021,131],{"class":82},[62,1023,1025],{"class":64,"line":1024},8,[62,1026,93],{"emptyLinePlaceholder":92},[62,1028,1030,1033,1036,1038,1040],{"class":64,"line":1029},9,[62,1031,1032],{"class":68},"if",[62,1034,1035],{"class":72}," start_date ",[62,1037,847],{"class":102},[62,1039,1005],{"class":72},[62,1041,1042],{"class":82},":\n",[62,1044,1046,1049,1051,1053,1056,1058],{"class":64,"line":1045},10,[62,1047,1048],{"class":122},"    print",[62,1050,126],{"class":82},[62,1052,173],{"class":172},[62,1054,1055],{"class":176},"The start date is earlier than the deadline.",[62,1057,173],{"class":172},[62,1059,131],{"class":82},[14,1061,823],{},[53,1063,1065],{"className":55,"code":1064,"language":57,"meta":58,"style":58},"True\nFalse\nThe start date is earlier than the deadline.\n",[18,1066,1067,1073,1078],{"__ignoreMap":58},[62,1068,1069],{"class":64,"line":65},[62,1070,1072],{"class":1071},"s39Yj","True\n",[62,1074,1075],{"class":64,"line":89},[62,1076,1077],{"class":1071},"False\n",[62,1079,1080,1083,1086,1089],{"class":64,"line":96},[62,1081,1082],{"class":72},"The start date ",[62,1084,1085],{"class":102},"is",[62,1087,1088],{"class":72}," earlier than the deadline",[62,1090,1091],{"class":82},".\n",[14,1093,1094],{},"Earlier dates are smaller than later dates.",[14,1096,1097,1098,1100,1101,1103],{},"Make sure both values are date objects. Do not compare a plain string like ",[18,1099,872],{}," with a ",[18,1102,316],{}," object.",[48,1105,1107],{"id":1106},"step-6-find-the-difference-between-dates","Step 6: Find the difference between dates",[14,1109,1110,1111,1103],{},"You can subtract one date from another. The result is a ",[18,1112,1113],{},"timedelta",[53,1115,1117],{"className":55,"code":1116,"language":57,"meta":58,"style":58},"from datetime import date\n\ntoday = date(2026, 4, 22)\ndue_date = date(2026, 5, 1)\n\ndifference = due_date - today\n\nprint(difference)\nprint(difference.days)\n",[18,1118,1119,1129,1133,1156,1181,1185,1200,1204,1215],{"__ignoreMap":58},[62,1120,1121,1123,1125,1127],{"class":64,"line":65},[62,1122,69],{"class":68},[62,1124,73],{"class":72},[62,1126,76],{"class":68},[62,1128,86],{"class":72},[62,1130,1131],{"class":64,"line":89},[62,1132,93],{"emptyLinePlaceholder":92},[62,1134,1135,1137,1139,1141,1143,1145,1147,1149,1151,1154],{"class":64,"line":96},[62,1136,99],{"class":72},[62,1138,103],{"class":102},[62,1140,106],{"class":112},[62,1142,126],{"class":82},[62,1144,378],{"class":180},[62,1146,83],{"class":82},[62,1148,957],{"class":180},[62,1150,83],{"class":82},[62,1152,1153],{"class":180}," 22",[62,1155,131],{"class":82},[62,1157,1158,1161,1163,1165,1167,1169,1171,1174,1176,1179],{"class":64,"line":119},[62,1159,1160],{"class":72},"due_date ",[62,1162,103],{"class":102},[62,1164,106],{"class":112},[62,1166,126],{"class":82},[62,1168,378],{"class":180},[62,1170,83],{"class":82},[62,1172,1173],{"class":180}," 5",[62,1175,83],{"class":82},[62,1177,1178],{"class":180}," 1",[62,1180,131],{"class":82},[62,1182,1183],{"class":64,"line":134},[62,1184,93],{"emptyLinePlaceholder":92},[62,1186,1187,1190,1192,1195,1197],{"class":64,"line":139},[62,1188,1189],{"class":72},"difference ",[62,1191,103],{"class":102},[62,1193,1194],{"class":72}," due_date ",[62,1196,381],{"class":102},[62,1198,1199],{"class":72}," today\n",[62,1201,1202],{"class":64,"line":156},[62,1203,93],{"emptyLinePlaceholder":92},[62,1205,1206,1208,1210,1213],{"class":64,"line":1024},[62,1207,123],{"class":122},[62,1209,126],{"class":82},[62,1211,1212],{"class":112},"difference",[62,1214,131],{"class":82},[62,1216,1217,1219,1221,1223,1225,1229],{"class":64,"line":1029},[62,1218,123],{"class":122},[62,1220,126],{"class":82},[62,1222,1212],{"class":112},[62,1224,109],{"class":82},[62,1226,1228],{"class":1227},"skxfh","days",[62,1230,131],{"class":82},[14,1232,823],{},[53,1234,1236],{"className":55,"code":1235,"language":57,"meta":58,"style":58},"9 days, 0:00:00\n9\n",[18,1237,1238,1261],{"__ignoreMap":58},[62,1239,1240,1243,1246,1248,1251,1253,1256,1258],{"class":64,"line":65},[62,1241,1242],{"class":180},"9",[62,1244,1245],{"class":72}," days",[62,1247,83],{"class":82},[62,1249,1250],{"class":180}," 0",[62,1252,481],{"class":82},[62,1254,1255],{"class":180},"00",[62,1257,481],{"class":82},[62,1259,1260],{"class":180},"00\n",[62,1262,1263],{"class":64,"line":89},[62,1264,1265],{"class":180},"9\n",[14,1267,192,1268,1271],{},[18,1269,1270],{},".days"," when you want just the number of days.",[14,1273,1274],{},"Here is a simple countdown example:",[53,1276,1278],{"className":55,"code":1277,"language":57,"meta":58,"style":58},"from datetime import date\n\ntoday = date.today()\ndeadline = date(2026, 5, 1)\n\ndays_left = (deadline - today).days\n\nprint(f\"Days left: {days_left}\")\n",[18,1279,1280,1290,1294,1308,1330,1334,1356,1360],{"__ignoreMap":58},[62,1281,1282,1284,1286,1288],{"class":64,"line":65},[62,1283,69],{"class":68},[62,1285,73],{"class":72},[62,1287,76],{"class":68},[62,1289,86],{"class":72},[62,1291,1292],{"class":64,"line":89},[62,1293,93],{"emptyLinePlaceholder":92},[62,1295,1296,1298,1300,1302,1304,1306],{"class":64,"line":96},[62,1297,99],{"class":72},[62,1299,103],{"class":102},[62,1301,106],{"class":72},[62,1303,109],{"class":82},[62,1305,113],{"class":112},[62,1307,116],{"class":82},[62,1309,1310,1312,1314,1316,1318,1320,1322,1324,1326,1328],{"class":64,"line":119},[62,1311,969],{"class":72},[62,1313,103],{"class":102},[62,1315,106],{"class":112},[62,1317,126],{"class":82},[62,1319,378],{"class":180},[62,1321,83],{"class":82},[62,1323,1173],{"class":180},[62,1325,83],{"class":82},[62,1327,1178],{"class":180},[62,1329,131],{"class":82},[62,1331,1332],{"class":64,"line":134},[62,1333,93],{"emptyLinePlaceholder":92},[62,1335,1336,1339,1341,1344,1346,1348,1351,1353],{"class":64,"line":139},[62,1337,1338],{"class":72},"days_left ",[62,1340,103],{"class":102},[62,1342,1343],{"class":82}," (",[62,1345,969],{"class":72},[62,1347,381],{"class":102},[62,1349,1350],{"class":72}," today",[62,1352,785],{"class":82},[62,1354,1355],{"class":1227},"days\n",[62,1357,1358],{"class":64,"line":156},[62,1359,93],{"emptyLinePlaceholder":92},[62,1361,1362,1364,1366,1369,1372,1375,1378,1381,1383],{"class":64,"line":1024},[62,1363,123],{"class":122},[62,1365,126],{"class":82},[62,1367,1368],{"class":850},"f",[62,1370,1371],{"class":176},"\"Days left: ",[62,1373,1374],{"class":180},"{",[62,1376,1377],{"class":112},"days_left",[62,1379,1380],{"class":180},"}",[62,1382,173],{"class":176},[62,1384,131],{"class":82},[202,1386],{},[48,1388,1390],{"id":1389},"common-beginner-mistakes","Common beginner mistakes",[14,1392,1393],{},"Here are some common problems when working with dates in Python:",[26,1395,1396,1404,1410,1413,1420],{},[29,1397,1398,1399,1401,1402],{},"Forgetting to import ",[18,1400,316],{}," or ",[18,1403,20],{},[29,1405,1406,1407,1409],{},"Using the wrong ",[18,1408,297],{}," format string",[29,1411,1412],{},"Mixing strings and date objects in comparisons",[29,1414,1415,1416,1419],{},"Calling ",[18,1417,1418],{},"datetime.today"," without parentheses",[29,1421,1422,1423],{},"Expecting formatted output without using ",[18,1424,291],{},[14,1426,1427],{},"Common causes include:",[26,1429,1430,1433,1438,1446,1449],{},[29,1431,1432],{},"Using a date string without converting it first",[29,1434,1435,1436],{},"Wrong format code in ",[18,1437,297],{},[29,1439,1440,1441,1443,1444],{},"Confusing ",[18,1442,316],{}," with ",[18,1445,20],{},[29,1447,1448],{},"Trying to compare text values instead of date objects",[29,1450,1451],{},"Forgetting that month and day order must match the format string",[14,1453,1454],{},"If something is not working, these simple checks can help:",[53,1456,1458],{"className":55,"code":1457,"language":57,"meta":58,"style":58},"python your_script.py\nprint(type(today))\nprint(type(user_input))\nprint(parsed_date)\nprint(parsed_date.strftime('%Y-%m-%d'))\n",[18,1459,1460,1470,1484,1499,1509],{"__ignoreMap":58},[62,1461,1462,1465,1467],{"class":64,"line":65},[62,1463,1464],{"class":72},"python your_script",[62,1466,109],{"class":82},[62,1468,1469],{"class":1227},"py\n",[62,1471,1472,1474,1476,1478,1480,1482],{"class":64,"line":89},[62,1473,123],{"class":122},[62,1475,126],{"class":82},[62,1477,814],{"class":813},[62,1479,126],{"class":82},[62,1481,113],{"class":112},[62,1483,189],{"class":82},[62,1485,1486,1488,1490,1492,1494,1497],{"class":64,"line":96},[62,1487,123],{"class":122},[62,1489,126],{"class":82},[62,1491,814],{"class":813},[62,1493,126],{"class":82},[62,1495,1496],{"class":112},"user_input",[62,1498,189],{"class":82},[62,1500,1501,1503,1505,1507],{"class":64,"line":119},[62,1502,123],{"class":122},[62,1504,126],{"class":82},[62,1506,802],{"class":112},[62,1508,131],{"class":82},[62,1510,1511,1513,1515,1517,1519,1521,1523,1525,1527,1529,1531],{"class":64,"line":134},[62,1512,123],{"class":122},[62,1514,126],{"class":82},[62,1516,802],{"class":112},[62,1518,109],{"class":82},[62,1520,167],{"class":112},[62,1522,126],{"class":82},[62,1524,859],{"class":172},[62,1526,177],{"class":176},[62,1528,181],{"class":180},[62,1530,859],{"class":172},[62,1532,189],{"class":82},[14,1534,1535],{},"These checks help you confirm:",[26,1537,1538,1541,1544],{},[29,1539,1540],{},"Whether a value is a string or a date object",[29,1542,1543],{},"Whether parsing worked correctly",[29,1545,1546],{},"Whether your formatted output looks the way you expect",[48,1548,1550],{"id":1549},"next-steps","Next steps",[14,1552,1553],{},"After this example, a good next step is to practice real tasks such as:",[26,1555,1556,1559,1562,1565],{},[29,1557,1558],{},"Adding timestamps to logs",[29,1560,1561],{},"Checking whether a deadline has passed",[29,1563,1564],{},"Parsing user-entered dates",[29,1566,1567],{},"Building a small due-date checker script",[14,1569,1570],{},"You may also want to learn:",[26,1572,1573,1577,1581,1585],{},[29,1574,1575],{},[303,1576,522],{"href":521},[29,1578,1579],{},[303,1580,903],{"href":902},[29,1582,1583],{},[303,1584,708],{"href":707},[29,1586,1587],{},[303,1588,1590],{"href":1589},"\u002Fhow-to\u002Fhow-to-format-strings-in-python\u002F","How to format strings in Python",[48,1592,1594],{"id":1593},"faq","FAQ",[1596,1597,1599],"h3",{"id":1598},"what-is-the-difference-between-date-and-datetime-in-python","What is the difference between date and datetime in Python?",[14,1601,1602,1604,1605,1607],{},[18,1603,316],{}," stores only the year, month, and day. ",[18,1606,20],{}," stores the date and the time.",[1596,1609,1611],{"id":1610},"how-do-i-print-a-date-in-a-different-format","How do I print a date in a different format?",[14,1613,192,1614,1616,1617,913,1619,917,1621,109],{},[18,1615,291],{}," with format codes like ",[18,1618,665],{},[18,1620,671],{},[18,1622,181],{},[1596,1624,1626],{"id":1625},"how-do-i-turn-a-string-into-a-date","How do I turn a string into a date?",[14,1628,192,1629,1631],{},[18,1630,717],{}," and make sure the format string matches the input text.",[1596,1633,1635],{"id":1634},"how-do-i-calculate-the-number-of-days-between-two-dates","How do I calculate the number of days between two dates?",[14,1637,1638,1639,1641],{},"Subtract one date from another and use the ",[18,1640,1270],{}," value from the result.",[1596,1643,1645],{"id":1644},"why-does-parsing-a-date-string-fail","Why does parsing a date string fail?",[14,1647,1648],{},"Usually the input text does not match the format string exactly.",[48,1650,1652],{"id":1651},"see-also","See also",[26,1654,1655,1659,1663,1667,1671],{},[29,1656,1657],{},[303,1658,306],{"href":305},[29,1660,1661],{},[303,1662,522],{"href":521},[29,1664,1665],{},[303,1666,903],{"href":902},[29,1668,1669],{},[303,1670,708],{"href":707},[29,1672,1673],{},[303,1674,896],{"href":895},[1676,1677,1678],"style",{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sx12J, html code.shiki .sx12J{--shiki-light:#F76D47;--shiki-light-font-style:inherit;--shiki-default:#B31D28;--shiki-default-font-style:italic;--shiki-dark:#FDAEB7;--shiki-dark-font-style:italic}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":58,"searchDepth":89,"depth":89,"links":1680},[1681,1682,1683,1684,1685,1686,1687,1688,1689,1690,1691,1692,1693,1700],{"id":50,"depth":89,"text":51},{"id":206,"depth":89,"text":207},{"id":227,"depth":89,"text":228},{"id":255,"depth":89,"text":256},{"id":309,"depth":89,"text":310},{"id":403,"depth":89,"text":404},{"id":525,"depth":89,"text":526},{"id":711,"depth":89,"text":712},{"id":906,"depth":89,"text":907},{"id":1106,"depth":89,"text":1107},{"id":1389,"depth":89,"text":1390},{"id":1549,"depth":89,"text":1550},{"id":1593,"depth":89,"text":1594,"children":1694},[1695,1696,1697,1698,1699],{"id":1598,"depth":96,"text":1599},{"id":1610,"depth":96,"text":1611},{"id":1625,"depth":96,"text":1626},{"id":1634,"depth":96,"text":1635},{"id":1644,"depth":96,"text":1645},{"id":1651,"depth":89,"text":1652},"Master python working with dates example in our comprehensive Python beginner guide.","md",{},"\u002Fexamples\u002Fpython-working-with-dates-example",{"title":5,"description":1701},"examples\u002Fpython-working-with-dates-example","1EZ-rO7HcJWagJfG7cd8BPo9eJ2I3LXPa6mY4_hO3GA",1777585478129]