1. 程式人生 > >將中國標準時間轉化為yyyy-MM-dd

將中國標準時間轉化為yyyy-MM-dd

// 將Thu May 12 2016 08:00:00 GMT+0800 (中國標準時間)轉化為yyyy-MM-dd

parseTime (str) {

if ((str + '').indexOf('-') != -1) {

str = str.replace(new RegExp(/-/gm), '/')

}

let d = new Date(str)

let newDateYear = d.getFullYear()

let newDateMonth = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1)

let newDateDay = d.getDate() + '' < 10 ? '0' + d.getDate() + '' : d.getDate() + ''

return newDateYear + '-' + newDateMonth + '-' + newDateDay

},