JS和vue中日期格式的轉換

 

1.獲取當前時間:vue

var now=new Date(); //Tue Oct 17 2017 18:08:40 GMT+0800 (中國標準時間)npm

 

獲取當前時間的日期orm

new Date().getDate()   //17get

 

new Date().toLocaleString()   //2017/10/17 下午6:08:40string

 

2.引用moment.js將標準時間轉化成YYYY-MM-DD hh:mm:ssconsole

var time=moment(new Date()).format("YYYY-MM-DD hh:mm:ss");form

//2017-10-17 06:08:40import

 

var time=moment(new Date()).format("DD");  //17引用

 

獲取當前時間的次日im

moment(new Date().setDate(new Date().getDate()+2)).format("YYYY-MM-DD hh:mm:ss")   //2017-10-19 06:08:40

 

3.將標準時間轉換成時間戳

new Date().getTime()    //1508234920433

 

將YYYY-MM-DD日期轉換成時間戳

var stringTime = "2014-07-10 10:21:12";

var timestamp2 = Date.parse(new Date(stringTime));

console.log( timestamp2 );// 1500286120000

 

4.將時間戳轉換成標準時間和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.vue中怎麼使用moment格式轉換日期?

  1)先安裝moment包:npm install moment

  2)在組件中引入moment: import moment from "moment"

  3)在組件中使用:let str = moment(new Date()).format("YYYY-MM-DD hh:mm:ss") //  2018-04-24 09:55:40

相關文章
相關標籤/搜索