{"id":553,"date":"2023-11-24T09:31:42","date_gmt":"2023-11-24T09:31:42","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=553"},"modified":"2023-11-27T05:14:12","modified_gmt":"2023-11-27T05:14:12","slug":"convert-string-to-date-javascript","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/convert-string-to-date-javascript\/","title":{"rendered":"Convert String to Date in JavaScript (with code)"},"content":{"rendered":"\n<p>In JavaScript, converting a string to a date is a common task when working with date values. Whether you&#8217;re parsing user input, manipulating dates, or storing them in a database, it&#8217;s crucial to understand the various methods available for converting strings to dates. In this article, we will cover the techniques to convert a String to Date Object in JavaScript. Let us get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What are Strings &amp; Date Objects?<\/strong><\/h2>\n\n\n\n<p>As we know, Strings represent a sequence of multiple characters. \u2018C\u2019 represents a single character and \u201cCat\u201d represents a string. In JavaScript, we can declare a string in two ways.<\/p>\n\n\n\n<p>One on the other side, the Date object represents a point in time in JavaScript. It can be used to work with dates and times. In further sections, we will see how to create Date Objects from Date Strings.<\/p>\n\n\n\n<p>The ISO 8601 calendar date extended format is the standard format maintained by the ISO This standard provides a well-defined and standard method of representing calendar dates and times in worldwide communications.\u00a0<\/p>\n\n\n\n<p>According to the <a href=\"https:\/\/www.iso.org\/iso-8601-date-and-time-format.html\" data-type=\"link\" data-id=\"https:\/\/www.iso.org\/iso-8601-date-and-time-format.html\" target=\"_blank\" rel=\"noopener\">8601 Calendar Format<\/a>, the Date is represented as yyyy-mm-dd.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3 Methods To Convert a String to Date in JavaScript<\/strong><\/h2>\n\n\n\n<p>Dates are a fundamental concept in programming, and conversion to date from string is an essential operation. In this section, we will discuss various methods to convert a string to date in JavaScript. Let us get started!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1) Using Date() Constructor<\/strong><\/h3>\n\n\n\n<p>The Date() is the constructor of the Date class. It allows us to create new date objects by taking a string as a parameter. <strong>The string passed as the parameter is converted into the date object given that the format of the string should be in the ISO 8601 standard format (yyyy-mm-dd).<\/strong> This sums up a supported and reliable conversion.<\/p>\n\n\n\n<p>The syntax of the same is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>let dateObject = Date(dateString);<\/code><\/pre>\n\n\n\n<p>where,&nbsp;<\/p>\n\n\n\n<p><strong>dateObject<\/strong>: variable to store the date object returned from the Date() function.<\/p>\n\n\n\n<p><strong>dateString<\/strong>: variable representing the string to be converted to the date object<\/p>\n\n\n\n<p>Let us take an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a Date String\nlet strDate='2023-11-17';\n\n\/\/printing the value and type of strDate\nconsole.log(strDate + &quot; &quot; + typeof(strDate));\n\n\/\/using the Date() constructor\nlet dateObj=new Date(strDate);\n\n\/\/printing the type of dateObj\nconsole.log(dateObj + &quot; &quot; + typeof(dateObj));<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>2023-11-17 string\nFri Nov 17 2023 05:30:00 GMT+0530 (India Standard Time) object\n<\/code><\/pre>\n\n\n\n<p>On observing the output carefully, we conclude that the Date String in the 8601 format has been converted to the Date object.<\/p>\n\n\n\n<p>Let us try to form a date object with some other date format (other than 8601).&nbsp;<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a Date String\nlet strDate='17-11-2023';\n\n\/\/printing the value and type of strDate\nconsole.log(strDate + &quot; &quot; + typeof(strDate));\n\n\/\/using the Date() constructor\nlet dateObj=new Date(strDate);\n\n\/\/printing the type of dateObj\nconsole.log(dateObj + &quot; &quot; + typeof(dateObj));<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>17-11-2023 string\nInvalid Date object\n<\/code><\/pre>\n\n\n\n<p>So, the output shows an invalid date object specifying that the acceptable format for the date string to be passed as a parameter is \u201cyyyy-mm-dd\u201d. On the other note, the output also shows an invalid date object when we pass an invalid month or invalid day.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2) Using Date.Parse() Method<\/strong><\/h3>\n\n\n\n<p>Another easy and straightforward method to convert string to date in JavaScript is by using the Date.parse() method. This method parses a date string. It returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.<\/p>\n\n\n\n<p>This numerical value is received from the Date.parse() method can be useful for various operations, such as date comparisons or calculations.\u00a0<\/p>\n\n\n\n<p>The syntax of the same is as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>let milliseconds= Date.parse(dateString);<\/code><\/pre>\n\n\n\n<p>where,&nbsp;<\/p>\n\n\n\n<p><strong>milliseconds<\/strong>: variable to store the milliseconds since January 1, 1970.<\/p>\n\n\n\n<p><strong>dateString<\/strong>: variable representing the string to be converted to no&nbsp; of milliseconds, can be of the format \u201cyear-month-date\u201d or \u201cyear\/month\/date\u201d<\/p>\n\n\n\n<p>Here\u2019s an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a Date String\nlet strDate='2023\/11\/17';\n\n\/\/printing the value and type of strDate\nconsole.log(strDate + &quot; &quot; + typeof(strDate));\n\n\/\/using the Date.parse() method\nlet millisecs=Date.parse(strDate);\n\n\/\/printing the milliseconds\nconsole.log(millisecs);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>17 Nov 2023 string\n1700159400000\n<\/code><\/pre>\n\n\n\n<p>Such a long numerical value represents the number of milliseconds that have elapsed since January 1, 1970. We have obtained the desired output.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3) Using the Date() Constructor with Different Parameters<\/strong><\/h3>\n\n\n\n<p>Both of the functions discussed above accept the string input in the 8061 extended format. If the date is not in the standard format, we use the below method to convert the string date to the date object.<\/p>\n\n\n\n<p>The Date class has another Date() constructor that takes parameterized input. The syntax of the same is given below:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>let dateObj = new Date(year, monthIndex, day, hours, minutes, seconds);\n<\/code><\/pre>\n\n\n\n<p>where,&nbsp;<\/p>\n\n\n\n<p><strong>dateObj<\/strong>: variable to store the date object returned from the Date() function<\/p>\n\n\n\n<p><strong>Year: <\/strong>all 4 digits of the year should be entered<\/p>\n\n\n\n<p><strong>monthIndex: <\/strong>index of the months starting from 0, Jan-0, Feb-1, Mar-2, and so on.<\/p>\n\n\n\n<p><strong>Day: <\/strong>enter the day value as it is (from 1 to 31)<\/p>\n\n\n\n<p><strong>hours: <\/strong>pass the value of hours in Railway time, eg. 1:00 pm, 2:00 pm, 3:00 pm will be taken as 13, 14, 15.&nbsp;<\/p>\n\n\n\n<p><strong>minutes: <\/strong>varies from 0 to 60<\/p>\n\n\n\n<p><strong>seconds: <\/strong>varies from 0 to 60<\/p>\n\n\n\n<p>Here\u2019s an example of the same:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/using the Date(year, monthInd, day, hours, minutes, seconds) method\nlet dateObj = new Date(2023, 2, 17, 8, 14, 20);\n\n\/\/printing the dateObj\nconsole.log(dateObj);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>2023-03-17T02:44:20.000Z<\/code><\/pre>\n\n\n\n<p>We have obtained the desired output.<\/p>\n\n\n\n<p>We can also <a href=\"https:\/\/favtutor.com\/articles\/string-to-array-javascript\/\">convert strings to array<\/a> using various methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion&nbsp;<\/strong><\/h2>\n\n\n\n<p>Converting a string to date in JavaScript is an essential skill for working with date values effectively. In this article, we explained different ways to do this conversion using Date() constructor and Date.Parse() method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to convert string to date object in Javascript using date constructor and parse method with code.<\/p>\n","protected":false},"author":9,"featured_media":555,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[9],"tags":[11,45,38],"class_list":["post-553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-javascript","tag-javascript-date","tag-javascript-strings"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=553"}],"version-history":[{"count":4,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/553\/revisions"}],"predecessor-version":[{"id":593,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/553\/revisions\/593"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/555"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}