Time format conversion

how to convert the time format of 2018-08-06T10:00:00.000Z to local time is similar to 2018-08-06 18:00:00

Mar.23,2022

first convert new Date ("2018-08-06T10:00:00.000Z") to get Mon Aug 06 2018 18:00:00 GMT+0800 (China Standard time) . Later, you can use moment or encapsulate a time conversion


directly use third-party libraries, such as moment (heavier) or date-fns (slightly lighter)


.

use it first:

var date = new Date('2018-08-06T10:00:00.000Z')
function formatDate(date) {
  var year = date.getFullYear()
  var month = format(date.getMonth() + 1)
  var da = format(date.getDate())
  var h = format(date.getHours())
  var m = format(date.getMinutes())
  var s = format(date.getSeconds())
  return year + '-' + month + '-' + da + ' ' + h + ':' + m + ':' + s
}
function format(val) {
  return Number(val) < 10 ? '0' + val : '' + val
}
console.log(formatDate(date))
Menu