{"id":4447,"date":"2016-03-02T09:36:13","date_gmt":"2016-03-02T09:36:13","guid":{"rendered":"http:\/\/www.hostingnotes.com\/?p=4447"},"modified":"2017-10-09T14:17:10","modified_gmt":"2017-10-09T14:17:10","slug":"date-and-time-in-javascript","status":"publish","type":"post","link":"https:\/\/www.webdevelopersnotes.com\/date-and-time-in-javascript","title":{"rendered":"Date and Time in JavaScript"},"content":{"rendered":"<p>The date object is very useful for obtaining the date and time on the client. It supports various methods, which return a specific value.<\/p>\n<p>To start working with dates and time, we first initialize a variable and assign it a value with the new operator and the Date() constructor. The main function of the new operator with Date() constructor is to create a new date object that is stored in the variable. Here is the code:<\/p>\n<pre class=\"small-text\">\r\nvar d = new Date();\r\n<\/pre>\n<p>Thus, variable d contains a new date object. We can now call the various methods of this date object.<\/p>\n<pre class=\"small-text\">\r\nvar t_date = d.<strong>getDate()<\/strong>;      \/\/ Returns the day of the month\r\nvar t_mon = d.<strong>getMonth()<\/strong>;      \/\/ Returns the month as a digit\r\nvar t_year = d.<strong>getFullYear()<\/strong>;  \/\/ Returns 4 digit year\r\nvar t_hour = d.<strong>getHours()<\/strong>;     \/\/ Returns hours\r\nvar t_min = d.<strong>getMinutes()<\/strong>;    \/\/ Returns minutes\r\nvar t_sec = d.<strong>getSeconds()<\/strong>;    \/\/ Returns seconds\r\nvar t_mil = d.<strong>getMilliseconds()<\/strong>;  \/\/ Returns Milliseconds\r\n<\/pre>\n<p>Now we can easily display the date, month and year in an alert box, using the values stored in respective variables.<\/p>\n<pre class=\"small-text\">\r\nalert(\"Today's date is \" + t_date + \"-\" + t_mon + \"-\" + t_year);\r\n<\/pre>\n<p><a href=\"javascript:void(0)\" onclick=\"var d = new Date();var t_date = d.getDate();var t_mon = d.getMonth();var t_year = d.getFullYear();var t_hour = d.getHours();var t_min = d.getMinutes();var t_sec = d.getSeconds();var t_mil = d.getMilliseconds();alert('Today\\'s date is ' + t_date + '-' + t_mon + '-' + t_year);\">Click here to display the alert box<\/a><\/p>\n<p>The eagle-eyed would have noticed that the month digit is one less than the present month. Hence, if you want to display the month, increment the value returned by getMonth() by 1. The corrected code should be:<\/p>\n<pre class=\"small-text\">\r\n<strong>t_mon++;<\/strong>\r\nalert(\"Today's date is \" + t_date + \"-\" + t_mon + \"-\" + t_year);\r\n<\/pre>\n<p><a href=\"javascript:void(0)\" onclick=\"var d = new Date();var t_date = d.getDate();var t_mon = d.getMonth();var t_year = d.getFullYear();var t_hour = d.getHours();var t_min = d.getMinutes();var t_sec = d.getSeconds();var t_mil = d.getMilliseconds();t_mon++;alert('Today\\'s date is ' + t_date + '-' + t_mon + '-' + t_year);\">Check the results<\/a><\/p>\n<p>Similarly, we can get the time using the variables that store hours, minutes, seconds and milliseconds values.<\/p>\n<pre class=\"small-text\">\r\nalert(\"The time is \" + t_hour + \":\" + t_min + \":\" + t_sec);\r\n<\/pre>\n<p><a href=\"javascript:void(0)\" onclick=\"var d = new Date();var t_date = d.getDate();var t_mon = d.getMonth();var t_year = d.getFullYear();var t_hour = d.getHours();var t_min = d.getMinutes();var t_sec = d.getSeconds();var t_mil = d.getMilliseconds();alert('The time is ' + t_hour + ':' + t_min + ':' + t_sec);\">Get time in an alert box<\/a><\/p>\n<p>In the next session we&#8217;ll see how we can display a greeting based on the client time.<\/p>\n<div class=\"pagenav\">\n<ul>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/browser-detection-through-javascript-navigator-object\" title=\"Detecting the browser using javascript - The javascipt navigator object\">&lt; Back<\/a><\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-tutorial\" title=\"JavaScript tutorial\">1<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-event-handlers-part-1\" title=\"Javascript onmouseover and javascript onmouseout event handlers\">10<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-if-statement\" title=\"Javascript IF statement\">20<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/date-and-time-in-javascript\" title=\"Displaying and printing date and time using javascript\" class=\"thispage\">23<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/changing-images-on-mouseover-using-javascript\" title=\"Changing images on mouseover using Javascript\">30<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-else-if\" title=\"Javascript ELSE IF statement\">Next &gt;<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"The date object is very useful for obtaining the date and time on the client. It supports various methods, which return a specific value. To start working with dates and time, we first initialize a variable and assign it a value with the new operator and the Date() constructor. The main function of the new [&hellip;]","protected":false},"author":1,"featured_media":15612,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[56],"tags":[],"class_list":["post-4447","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/posts\/4447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/comments?post=4447"}],"version-history":[{"count":0,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/posts\/4447\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/media\/15612"}],"wp:attachment":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/media?parent=4447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/categories?post=4447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/tags?post=4447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}