{"id":1206,"date":"2021-07-06T06:36:48","date_gmt":"2021-07-06T06:36:48","guid":{"rendered":"https:\/\/usemynotes.com\/?p=1206"},"modified":"2023-06-14T15:41:06","modified_gmt":"2023-06-14T15:41:06","slug":"date-in-javascript","status":"publish","type":"post","link":"https:\/\/usemynotes.com\/date-in-javascript\/","title":{"rendered":"Date in JavaScript | How to create a date object in JavaScript?"},"content":{"rendered":"<p>Hey guys, welcome back, in this module, I will discuss <strong>date in JavaScript<\/strong>. In the previous modules, we learned objects in JavaScript, so if you don\u2019t know what objects in JavaScript are, you can see our previous module. So, let\u2019s begin.<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-1207 size-full\" src=\"https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/date-in-javascript.png\" alt=\"Date in JavaScript\" width=\"1000\" height=\"700\" title=\"\" srcset=\"https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/date-in-javascript.png 1000w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/date-in-javascript-300x210.png 300w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/date-in-javascript-768x538.png 768w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/date-in-javascript-150x105.png 150w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/date-in-javascript-696x487.png 696w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/date-in-javascript-600x420.png 600w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/date-in-javascript-100x70.png 100w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/usemynotes.com\/date-in-javascript\/#Date_in_JavaScript\" >Date in JavaScript<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/usemynotes.com\/date-in-javascript\/#How_to_create_a_date_object_in_JavaScript\" >How to create a date object in JavaScript?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/usemynotes.com\/date-in-javascript\/#Date_Methods_in_JavaScript\" >Date Methods in JavaScript<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Date_in_JavaScript\"><\/span>Date in JavaScript<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In JavaScript, a date object is used to get the date (year, month, day) and time (hours, minutes, seconds, milliseconds). Using date objects in JavaScript, you can display the timer and date on your web pages. By default, JavaScript can show a date as a full-text <a href=\"https:\/\/usemynotes.com\/what-are-data-types-in-javascript\/\">string<\/a> using the client\u2019s browser time zone.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_create_a_date_object_in_JavaScript\"><\/span>How to create a date object in JavaScript?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>JavaScript date objects can be created using the Date() constructor in the following ways:<\/p>\n<ol>\n<li><strong>new Date()<\/strong><br \/>\nUsing this method, we will get the client\u2019s machine\u2019s current date and time.<br \/>\n<strong>Syntax<\/strong><br \/>\nvar variable_name=new Date();<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">let obj=new Date();\r\nconsole.log(obj);<\/pre>\n<\/li>\n<li><strong>new Date<\/strong>(millisec)\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">let obj=new Date(2000);\r\nconsole.log(obj);<\/pre>\n<p>In the above example, 2000 milliseconds are equal to 2 seconds, since 1 second= 1000 milliseconds.<\/li>\n<li><strong>new Date<\/strong>(string)<br \/>\nIn this method, a date in the form of a string is passed as a parameter.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">let d = new Date(&quot;July 06 2021 11:13:00&quot;);\r\nconsole.log(d);<\/pre>\n<\/li>\n<li><strong>new Date(year, month, date, hour, min, sec, millisecond)<\/strong><br \/>\nThis method is used for creating date objects with specified date-time. In <a href=\"https:\/\/usemynotes.com\/javascript\">JavaScript<\/a>, the numbering of months starts from 0 to 11. Thus, January is 0, February is 1, \u2026.., December is 11.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">let e=new Date(2020, 4, 23, 15, 45, 30, 1000);\r\nconsole.log(e);\r\n\r\nlet d=new Date(2019, 3, 2, 10, 5, 10);\r\nconsole.log(d);\r\n\r\nSpecifying only year, month and day.\r\nconst date=new Date(2021,5,4);\r\nconsole.log(date);\r\n\r\nSpecifying only year and month\r\nconst date=new Date(2021,5);\r\nconsole.log(date);<\/pre>\n<\/li>\n<\/ol>\n<h2><span class=\"ez-toc-section\" id=\"Date_Methods_in_JavaScript\"><\/span>Date Methods in JavaScript<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>JavaScript date object provides a set of methods for getting and setting the values of year, month, day, hours, minutes, seconds, and milliseconds. Let\u2019s discuss them in detail.<\/p>\n<ol>\n<li><strong>getDate<\/strong>()<br \/>\nIt is a method used to return an integer value between 1 to 31 based on the current date or specified date.<br \/>\n<strong>Syntax<\/strong><br \/>\nDate_object.getDate()<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">let date=new Date();\r\nconsole.log(date.getDate());\r\n\r\n\/\/Output\r\n\/\/22<\/pre>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">let date=new Date(2021,6,10);\r\nconsole.log(date.getDate());\r\n\r\n\/\/Output\r\n\/\/10<\/pre>\n<\/li>\n<li><strong>getFullYear<\/strong>()<br \/>\nIt is used to return the current year or the year you have specified.<br \/>\n<strong>Syntax<\/strong><br \/>\nDate_object.getFullyear()<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">var date=new Date(2019,6,10);\r\nconsole.log(date.getFullYear());\r\n\r\nvar d=new Date();\r\nconsole.log(d.getFullYear());\r\n\r\n\/\/Output\r\n\/\/2019\r\n\/\/2021<\/pre>\n<\/li>\n<li><strong>getMonth<\/strong>()<br \/>\nThis method is used to return an integer value between 0 to 11 based on the current date or given date.<br \/>\n<strong>Syntax<\/strong><br \/>\nDate_object.getMonth()<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">var date=new Date(2020,6,8);\r\nconsole.log(date.getMonth());\r\nvar d=new Date();\r\nconsole.log(d.getMonth());\r\nconsole.log(d);\r\n\r\n\/\/Output\r\n\/\/6\r\n\/\/4\r\n\/\/Sat May 22 2021 18:39:25 GMT+0530 (India Standard Time)<\/pre>\n<\/li>\n<li><strong>getDay<\/strong>()<br \/>\nIt is used to get the day of the week and returns an integer value between 0 and 6. Thus, Sunday is 0, Monday is 1, \u2026.., Saturday is 6.<br \/>\n<strong>Syntax<\/strong><br \/>\nDate_object.getDate()<br \/>\nvar date=new Date();<br \/>\nconsole.log(date.toDateString())<br \/>\nconsole.log(date.getDay());<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">var d=new Date(2020,10,4);\r\nconsole.log(d.toString());\r\nconsole.log(d.getDay());\r\n\r\nvar obj=new Date(&quot;April 20 1999&quot;);\r\nconsole.log(obj.toDateString());\r\nconsole.log(obj.getDay());\r\n\r\n\/\/Output\r\n\/\/Sat May 22 2021\r\n\/\/6\r\n\/\/Wed Nov 04 2020 00:00:00 GMT+0530 (India Standard Time)\r\n\/\/3\r\n\/\/Tue Apr 20 1999\r\n\/\/2<\/pre>\n<\/li>\n<li><strong>getHours<\/strong>()<br \/>\nIt is used to return an integer value between 0 and 23 based on the local time or specified time.<br \/>\n<strong>Syntax<\/strong><br \/>\nDate_object.getHours()<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">var date=new Date();\r\nconsole.log(date.toDateString())\r\nconsole.log(date.getHours());\r\n\r\nvar d=new Date(2020,8,11,14);\r\nconsole.log(d.toDateString());\r\nconsole.log(d.getHours());\r\n\r\n\/\/Output\r\n\/\/Sat May 22 2021\r\n\/\/19\r\n\/\/Fri Sep 11 2020\r\n\/\/14<\/pre>\n<\/li>\n<li><strong>getMinutes<\/strong>()<br \/>\nA date method is used to return an integer value between 0 and 59 based on the local time or specified time.<br \/>\n<strong>Syntax<\/strong><br \/>\nDate_object.getMinutes()<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">var date=new Date();\r\nconsole.log(date.toDateString())\r\nconsole.log(date.getMinutes());\r\n\r\nvar d=new Date(2021,9,11,10,45);\r\nconsole.log(d.toDateString());\r\nconsole.log(d.getMinutes());\r\n\r\n\/\/Output\r\n\/\/Sat May 22 2021\r\n\/\/ 6\r\n\/\/Mon Oct 11 2021\r\n\/\/45<\/pre>\n<\/li>\n<li><strong>getSeconds<\/strong>()<br \/>\nThis date method is used to return an integer value between 0 and 60 based on the local time or specified time.<b><br \/>\nSyntax<br \/>\n<\/b>Date_object.getSeconds()<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">var date=new Date();\r\nconsole.log(date.toDateString())\r\nconsole.log(date.getSeconds());\r\n\r\nvar d=new Date(2021,9,11,10,45,32);\r\nconsole.log(d.toDateString());\r\nconsole.log(d.getSeconds());\r\n\r\n\/\/Output\r\n\/\/Sat May 22 2021\r\n\/\/50\r\n\/\/Mon Oct 11 2021\r\n\/\/32<\/pre>\n<\/li>\n<li><strong>setDate<\/strong>()<br \/>\nThis method is used to set the date to a specified value between 1 and 31.<br \/>\n<strong>Syntax<\/strong><br \/>\nDate_object.setDate(value)<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">var date= new Date();\r\nconsole.log(date.getDate());\r\ndate.setDate(20);\r\nconsole.log(date.getDate())\r\n\r\n\/\/Output\r\n\/\/22\r\n\/\/20<\/pre>\n<\/li>\n<li><strong>setMonth<\/strong>()<br \/>\nThis method is used to set the month to a specified value between 0 and 11.<br \/>\n<strong>Syntax<\/strong><br \/>\nDate_object.setMonth(value)<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">var date= new Date();\r\nconsole.log(date.getMonth());\r\ndate.setMonth(1);\r\nconsole.log(date.getMonth());\r\n\r\n\/\/Output\r\n\/\/4\r\n\/\/1<\/pre>\n<\/li>\n<li><strong>setFullYear<\/strong>()<br \/>\nIt is used to set the year to a specified value.<br \/>\n<strong>Syntax<\/strong><br \/>\nDate_object.setFullYear(value)<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">var date= new Date();\r\nconsole.log(date.getFullYear());\r\ndate.setFullYear(1999);\r\nconsole.log(date.getFullYear());\r\n\r\n\/\/Output\r\n\/\/2021\r\n\/\/1999<\/pre>\n<\/li>\n<li><strong>setHours<\/strong>()<br \/>\nThis method sets the hour value to a specified value.<br \/>\n<strong>Syntax<\/strong><br \/>\nDate_object.setHours(value)<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">var date= new Date();\r\nconsole.log(date.getHours());\r\ndate.setHours(16);\r\nconsole.log(date.getHours());\r\n\r\n\/\/Output\r\n\/\/19\r\n\/\/16<\/pre>\n<\/li>\n<li><strong>setMinutes<\/strong>()<br \/>\nThis date method sets the value of a minute to a specified value.<br \/>\n<strong>Syntax<\/strong><br \/>\nDate_object.setMinutes(value)<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">var date= new Date();\r\nconsole.log(date.getMinutes());\r\ndate.setMinutes(49);\r\nconsole.log(date.getMinutes());\r\n\r\n\/\/Output\r\n\/\/40\r\n\/\/49<\/pre>\n<\/li>\n<\/ol>\n<p>There are many other date methods in JavaScript to get and set the date and time based on universal time.<\/p>\n<p>I hope <strong>Date in JavaScript<\/strong> module has helped you a lot in gaining information about the <strong>date object in JavaScript<\/strong>. It will be clearer when you will implement it by yourself. Keep in touch with us for more modules like this. Keep coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey guys, welcome back, in this module, I will discuss date in JavaScript. In the previous modules, we learned objects in JavaScript, so if you don\u2019t know what objects in JavaScript are, you can see our previous module. So, let\u2019s begin. Date in JavaScript In JavaScript, a date object is used to get the date [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1207,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":{"0":"post-1206","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-javascript"},"_links":{"self":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/posts\/1206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/comments?post=1206"}],"version-history":[{"count":0,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/posts\/1206\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/media\/1207"}],"wp:attachment":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/media?parent=1206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/categories?post=1206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/tags?post=1206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}