javascript converts standard time into year-month-day, hour-minute-second, "yyyy-MM-dd HH:mm:ss" format-record

javascript convert standard time into "yyyy-MM-dd HH:mm:ss" format


The first to convert standard time to "yyyy-MM-dd" format

which is:year month day
The obtained time format is: Thu Nov 12 2020 17:27:02 GMT+0800 (China Standard Time);
var date = new Date();
// 核心代码,可自行封装
function converTimeOfYMD(date) {
    
    
  return new Date(date).toISOString().slice(0, 10);
}
console.log(converTimeOfYMD(date))

Conversion result: 2020-11-12

The second way to convert the standard time into "yyyy-MM-dd HH:mm:ss" format

which is:Year-month-day hour-minute-second
The obtained time format is: Thu Nov 12 2020 17:27:02 GMT+0800 (China Standard Time);
var date = new Date();
// 核心代码,可自行封装
function converTimeOfHMS(date) {
    
    
  var json_date = new Date(date).toJSON();
  return new Date(new Date(json_date) + 8 * 3600 * 1000)
    .toISOString()
    .replace(/T/g, " ")
    .replace(/\.[\d]{3}Z/, "");
}
console.log(converTimeOfHMS(date))

The conversion result is: 2020-11-12 17:27:02.

Guess you like

Origin blog.csdn.net/m0_46442996/article/details/109648864
Recommended