Vuejs method to convert string to date: 1. Install the moment package; 2. Introduce moment into the component; 3. Use "let str = moment(new Date()).format("YYYY-MM -DD hh:mm:ss")" method can be used to convert.
The operating environment of this article: Windows 7 system, Vue2.9.6 version, DELL G3 computer
How does vuejs convert a string into a date?
Conversion of date formats in JS and vue:
1. Get the current time:
var now=new Date(); //Tue Oct 17 2017 18:08:40 GMT+0800 (中国标准时间)
Get the date of the current time
new Date().getDate() //17 new Date().toLocaleString() //2017/10/17 下午6:08:40
2. Quote moment.js to convert the standard time into YYYY-MM-DD hh:mm:ss
var time=moment(new Date()).format("YYYY-MM-DD hh:mm:ss"); //2017-10-17 06:08:40 var time=moment(new Date()).format("DD"); //17
Get the current time Two days
moment(new Date().setDate(new Date().getDate()+2)).format("YYYY-MM-DD hh:mm:ss") //2017-10-19 06:08:40
3. Convert standard time to timestamp
new Date().getTime() //1508234920433
Convert YYYY-MM-DD date to timestamp
var stringTime = "2014-07-10 10:21:12"; var timestamp2 = Date.parse(new Date(stringTime)); console.log( timestamp2 );// 1500286120000
4. Convert timestamp to standard time and YYYY-MM-DD
new Date(parseInt('1508234920433')) //Tue Oct 17 2017 18:08:40 GMT+0800 (中国标准时间) moment(new Date(parseInt('1508234920433'))).format("YYYY-MM-DD hh:mm:ss") //2017-10-19 06:08:40
5. How to use moment format to convert date in vue?
1) Install the moment package first: npm install moment
2)Introduce moment into the component: import moment from "moment"
3)Use in the component: let str = moment(new Date()).format("YYYY-MM-DD hh:mm:ss") // 2018-04-24 09:55:40
Recommended study: " The latest 5 vue.js video tutorial selections》
The above is the detailed content of How to convert string to date in vuejs. For more information, please follow other related articles on the PHP Chinese website!