The new Date() will return a date object as rendered by the date string passed as an argument. If you are not passing the parameter in the new Date() function. The new Date() returns the current system date and time, including the time zone information of your local system. Following is the syntax of the Date constructor − var date = new Date('date_string') The most commonly used way to convert a string into a date object is using the constructor of the Date() class. Let us see these solutions with examples − Using the Date() constructor Using the Date.parse() method − Same as the Date constructor this method accepts a string value parses and returns the date value in the form of milliseconds. Using the constructor of the Date class − This constructor accepts a string value representing the date value, converts it into a Date object, and returns the result. These powerful new features will modernize your JavaScript with shorter and more expressive code.In this article, we are going to discuss how to convert a string value to a Date object in JavaScript. This guide will bring you up to speed with all the latest features added in ECMAScript 13. The date-fns NPM package provides a parse() function to easily convert strings of various formats to a Date object.ġ1 Amazing New JavaScript Features in ES13.Use the toISOString() method to convert a Date object to an ISO-8601 string for consistent parsing behavior.For non-ISO-8601 date formats, we can split the string and pass the date components as numbers to the Date() constructor.Invalid date formats will result in an error being thrown or an invalid date being created.To convert a string to a Date object in JavaScript, call the Date() constructor and pass the string as an argument.Here we specify that the string is of the MM-dd-yyyy hh:m:ss format so that it can be converted correctly.Ĭonst date = parse(str, 'MM-dd-yyyy hh:m:ss', new Date()) Ĭonsole.log(date) // T08:13:50.000Z Key takeaways We can also use parse() function from the date-fns NPM package to easily convert strings of a wide variety of formats to a Date object. The Date toISOString() method returns a string of the date in the ISO 8601 format according to universal time. Can convert back to Date object with browser-independent parsingĬonsole.log(sameDate.getMinutes()) // 13 We can do this with the toISOString() method.Ĭonsole.log(isoString) // T08:13:50.000Z If we want to convert a Date object to a string for storage in a file or database, we can store it in the ISO 8601 format, so that we can retrieve and easily convert it back to a Date object with browser-independent parsing behavior without using any third-party libraries. How to Convert a Date Object to an ISO 8601 String Similarly, the values in the time string were separated with a colon ( :), so this was the separator we used to separate them with the String split() method.Īfter obtaining each date and time value separately, we converted them to numbers with the unary operator ( +) and passed them to the Date() constructor. So we used this as the separator when calling split() to obtain the month, day, and year individually. Unlike in the previous example, this time, the values in the date string were separated with a hyphen ( -). Const = str.split(' ') Ĭonst = dateStr.split('-') Ĭonst = timeStr.split(':') įirst, we had to split the string with a space to use the date and time strings separately.
0 Comments
Leave a Reply. |