{"id":1102,"date":"2026-11-13T12:04:21","date_gmt":"2026-11-13T12:04:21","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=1102"},"modified":"2026-11-13T12:04:28","modified_gmt":"2026-11-13T12:04:28","slug":"javascript-date-objects","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/javascript-date-objects\/","title":{"rendered":"JavaScript Date Object: Create, Read, Change and Format Dates"},"content":{"rendered":"<p>Hello friends! Almost every app needs dates somewhere, whether it is showing today&#8217;s date, counting days to an exam or printing the time on a message. JavaScript&#8217;s built-in <code>Date<\/code> object lets you work with dates and times: today&#8217;s date, a countdown to an exam, a person&#8217;s age, or a timestamp on a message. Inside, a Date is just one number, the milliseconds since 1 January 1970 (UTC). The methods let you read and change that moment in human terms.<\/p>\n<p>In the examples below, outputs that depend on the time zone are shown for Indian Standard Time (IST, UTC+5:30). So let&#8217;s begin with the very first step: creating a date.<\/p>\n<h2>Creating a Date<\/h2>\n<pre class=\"wp-block-code\"><code>const d1 = new Date(2026, 0, 26);            \/\/ year, month (0-based), day\nconsole.log(d1.toDateString());\n\nconst d2 = new Date(2026, 0, 26, 9, 30);     \/\/ with hours and minutes\nconsole.log(d2.toString());\n\nconst d3 = new Date(\"2026-08-15\");           \/\/ ISO date string (treated as UTC)\nconsole.log(d3.toISOString());\n\nconst d4 = new Date(\"2026-08-15T10:00:00+05:30\");\nconsole.log(d4.toISOString());\n\nconst d5 = new Date(0);                       \/\/ milliseconds since 1 Jan 1970 UTC\nconsole.log(d5.toISOString());<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Mon Jan 26 2026\nMon Jan 26 2026 09:30:00 GMT+0530 (India Standard Time)\n2026-08-15T00:00:00.000Z\n2026-08-15T04:30:00.000Z\n1970-01-01T00:00:00.000Z<\/code><\/pre>\n<p><code>new Date()<\/code> with no arguments gives the current date and time. <strong>Months start from 0<\/strong>: January is 0 and December is 11. This catches almost every beginner at least once.<\/p>\n<p>Once you have a Date, the next thing you will usually want is to pull out a part of it, like just the year or just the hour.<\/p>\n<h2>Reading Parts of a Date (Getters)<\/h2>\n<pre class=\"wp-block-code\"><code>const d = new Date(2026, 10, 14, 18, 45, 30);   \/\/ 14 Nov 2026, 6:45:30 PM\nconsole.log(d.getFullYear(), d.getMonth(), d.getDate(), d.getDay());\nconsole.log(d.getHours(), d.getMinutes(), d.getSeconds());\nconsole.log(d.getTime());\n\nconst days = [\"Sun\", \"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\"];\nconsole.log(days[d.getDay()]);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>2026 10 14 6\n18 45 30\n1794662130000\nSat<\/code><\/pre>\n<table>\n<thead>\n<tr>\n<th>Method<\/th>\n<th>Returns<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>getFullYear()<\/code><\/td>\n<td>4-digit year<\/td>\n<\/tr>\n<tr>\n<td><code>getMonth()<\/code><\/td>\n<td>0 to 11<\/td>\n<\/tr>\n<tr>\n<td><code>getDate()<\/code><\/td>\n<td>day of the month, 1 to 31<\/td>\n<\/tr>\n<tr>\n<td><code>getDay()<\/code><\/td>\n<td>day of the week, 0 (Sunday) to 6 (Saturday)<\/td>\n<\/tr>\n<tr>\n<td><code>getHours()<\/code>, <code>getMinutes()<\/code>, <code>getSeconds()<\/code><\/td>\n<td>time parts<\/td>\n<\/tr>\n<tr>\n<td><code>getTime()<\/code><\/td>\n<td>milliseconds since 1 Jan 1970 UTC<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Each getter also has a UTC version, such as <code>getUTCHours()<\/code>, which ignores your local time zone.<\/p>\n<p>Reading is one half of the story. The other half is changing a date, for example moving it 5 days ahead.<\/p>\n<h2>Changing a Date (Setters)<\/h2>\n<pre class=\"wp-block-code\"><code>const f = new Date(2026, 11, 30);\nf.setDate(f.getDate() + 5);         \/\/ add 5 days\nconsole.log(f.toDateString());\n\nconst e = new Date(2026, 0, 31);\ne.setMonth(1);                      \/\/ try to move 31 Jan to February\nconsole.log(e.toDateString());<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Mon Jan 04 2027\nTue Mar 03 2026<\/code><\/pre>\n<p>Dates roll over automatically. Adding 5 days to 30 December correctly moves into January of the next year. But there is no 31 February, so in the second example JavaScript rolls forward to 3 March. Keep this in mind when you add months.<\/p>\n<p>Now comes a part that matters a lot for us in India: showing the date in the format people here are used to.<\/p>\n<h2>Formatting Dates for Indian Users<\/h2>\n<pre class=\"wp-block-code\"><code>const g = new Date(2026, 7, 15, 9, 5);\nconsole.log(g.toLocaleDateString(\"en-IN\"));\nconsole.log(g.toLocaleDateString(\"en-IN\", { day: \"numeric\", month: \"long\", year: \"numeric\" }));\nconsole.log(g.toLocaleTimeString(\"en-IN\", { hour: \"2-digit\", minute: \"2-digit\" }));\nconsole.log(g.toLocaleDateString(\"hi-IN\", { weekday: \"long\", day: \"numeric\", month: \"long\" }));<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>15\/8\/2026\n15 August 2026\n09:05 am\n\u0936\u0928\u093f\u0935\u093e\u0930, 15 \u0905\u0917\u0938\u094d\u0924<\/code><\/pre>\n<p><code>toLocaleDateString()<\/code> with the <code>\"en-IN\"<\/code> locale gives the day-month-year order used in India, and <code>\"hi-IN\"<\/code> even gives Hindi day and month names.<\/p>\n<p>Friends, you now know enough to build something useful. Let&#8217;s try two small programs.<\/p>\n<h2>Practical Examples<\/h2>\n<h3>1. Days Left Until an Exam<\/h3>\n<pre class=\"wp-block-code\"><code>const start = new Date(2026, 10, 9);   \/\/ 9 Nov 2026\nconst exam = new Date(2027, 1, 15);    \/\/ 15 Feb 2027\nconst ms = exam - start;\nconsole.log(Math.round(ms \/ (1000 * 60 * 60 * 24)));<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>98<\/code><\/pre>\n<p>Subtracting two dates gives the difference in milliseconds. Divide by the milliseconds in a day to get days.<\/p>\n<h3>2. Calculate Age<\/h3>\n<pre class=\"wp-block-code\"><code>function age(dob, today) {\n  let a = today.getFullYear() - dob.getFullYear();\n  const m = today.getMonth() - dob.getMonth();\n  if (m &lt; 0 || (m === 0 &amp;&amp; today.getDate() &lt; dob.getDate())) a--;\n  return a;\n}\nconsole.log(age(new Date(2008, 11, 20), new Date(2026, 10, 9)));<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>17<\/code><\/pre>\n<p>The birthday (20 December) has not come yet in 2026, so one year is subtracted.<\/p>\n<h2>Common Mistakes<\/h2>\n<p>Dates are one area where even experienced programmers slip up. Here are the mistakes to watch out for:<\/p>\n<pre class=\"wp-block-code\"><code>const bad = new Date(\"31\/12\/2026\");      \/\/ DD\/MM\/YYYY is not understood\nconsole.log(bad.toString(), isNaN(bad));\n\nconsole.log(new Date(2026, 1, 30).toDateString());   \/\/ 30 February?\n\nconst a1 = new Date(2026, 0, 1);\nconst a2 = new Date(2026, 0, 1);\nconsole.log(a1 == a2, a1.getTime() === a2.getTime());<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Invalid Date true\nMon Mar 02 2026\nfalse true<\/code><\/pre>\n<ul>\n<li><strong>Indian date format strings are not parsed.<\/strong> Use <code>new Date(2026, 11, 31)<\/code> or the ISO format <code>\"2026-12-31\"<\/code>. Check for <code>isNaN(date)<\/code> when reading user input.<\/li>\n<li><strong>Invalid days do not throw errors;<\/strong> they roll over silently, as 30 February became 2 March here.<\/li>\n<li><strong>Comparing dates with <code>==<\/code> compares objects, not moments.<\/strong> Use <code>getTime()<\/code>, or <code>&lt;<\/code> and <code>&gt;<\/code>, which work directly on dates.<\/li>\n<li><strong>&#8220;2026-08-15&#8221; (date only) is read as UTC midnight,<\/strong> while <code>\"2026-08-15T00:00\"<\/code> (with a time) is read as local time. Be consistent.<\/li>\n<\/ul>\n<p>So friends, the three things to remember are simple. Months start from 0, so November is 10 and you add 1 when you display it. <code>new Date()<\/code> gives you today&#8217;s date, and <code>toLocaleDateString(\"en-IN\")<\/code> shows it the Indian way. And to find the days between two dates, subtract them and divide by <code>1000 * 60 * 60 * 24<\/code>, then round the result. Practise with your own birthday or your next exam date, and the Date object will soon feel easy.<\/p>\n<p>Related: <a href=\"https:\/\/gyanipandit.com\/programming\/javascript-map-keys\/\">JavaScript Map keys()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/javascript-set-has\/\">JavaScript Set has()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/arrow-functions-javascript\/\">Arrow functions in JavaScript<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello friends! Almost every app needs dates somewhere, whether it is showing today&#8217;s date, counting days to an exam or printing the time on a message. JavaScript&#8217;s built-in Date object lets you work with dates and times: today&#8217;s date, a countdown to an exam, a person&#8217;s age, or a timestamp on a message. Inside, a &#8230; <a title=\"JavaScript Date Object: Create, Read, Change and Format Dates\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/javascript-date-objects\/\" aria-label=\"Read more about JavaScript Date Object: Create, Read, Change and Format Dates\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[14,15],"class_list":["post-1102","post","type-post","status-publish","format-standard","hentry","category-javascript-tutorial","tag-javascript","tag-javascript-tutorial"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=1102"}],"version-history":[{"count":5,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1102\/revisions"}],"predecessor-version":[{"id":6135,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1102\/revisions\/6135"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=1102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=1102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=1102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}