Home >Web Front-end >Front-end Q&A >string转date javascript
Converting string to date in JavaScript is a common need. We usually convert the user-entered date string into an actual date object so that we can process and calculate it. In this article, we will explain how to convert a string to a date using JavaScript.
Convert a string to a date
JavaScript provides a built-in method to convert a string into a date object. We can convert the string to a timestamp using the Date.parse() method and then convert it to a date object using the new Date() method.
The following code demonstrates how to convert a date string into a date object:
let dateString = "2022-08-01"; let dateObject = new Date(Date.parse(dateString)); console.log(dateObject);
In the above code, we pass the string "2022-08-01" to Date. parse() method and assigns its return value to the Date object. We can then convert the timestamp to a date object by using the constructor.
The output results are as follows:
Mon Aug 01 2022 00:00:00 GMT+0800 (中国标准时间)
You can find that we have successfully converted the string into a date object!
Convert a string with time to date
If your date string contains time information, then you can also convert it to a date object. The following code demonstrates how to convert a string with time information into a date object:
let dateString = "2022-08-01T12:30:00.000Z"; let dateObject = new Date(dateString); console.log(dateObject);
In this example, we convert the string with time information "2022-08-01T12:30:00.000 Z" is passed to the constructor of the Date object. The constructor converts a string directly into a date object without using the Date.parse() method.
The output results are as follows:
Mon Aug 01 2022 20:30:00 GMT+0800 (中国标准时间)
You can see that the Date object has successfully parsed the time information in the date string.
Use moment.js library to convert strings
In addition to using JavaScript’s built-in methods, you can also use the third-party library moment.js to convert date strings. The moment.js library makes it easier for you to parse and format date strings.
The following code demonstrates how to convert a date string to a date object using moment.js:
let dateString = "2022-08-01T12:30:00.000Z"; let dateObject = moment(dateString); console.log(dateObject);
In the above code, we pass the date string to the moment() function, and assign the returned value to a variable. This variable is a moment.js object, which can be converted to a JavaScript Date object by calling the toDate() method.
The output results are as follows:
Mon Aug 01 2022 20:30:00 GMT+0800 (中国标准时间)
You can find that the moment.js library provides a concise and easy-to-use way to convert date strings into Date objects.
Summary
Converting a string to a date object is a common need in JavaScript programming. We can convert a date string to a timestamp using the built-in Date.parse() method and then convert the timestamp to a Date object by using the new Date() constructor. We can also use the moment.js library to parse and format date strings more conveniently. No matter which method you choose, you must understand the format and time zone information of the date string to ensure that the string is parsed correctly.
The above is the detailed content of string转date javascript. For more information, please follow other related articles on the PHP Chinese website!