js format about date

First: convert timestamps to standard times

var date = new Date(time stamp); //Get a time object
function format(fmt, date) {
                var o = {
                    "M+": date.getMonth() + 1, //Month   
                    "d+": date.getDate(), //day   
                    "h+": date.getHours(), //hour   
                    "m+": date.getMinutes(), //branch   
                    "s+": date.getSeconds(), //second   
                    "q+": Math.floor((date.getMonth() + 3) / 3), //quarter   
                    "S": date.getMilliseconds() //Millisecond   
                };
                if (/(y+)/.test(fmt))
                    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
                for (var k in o)
                    if (new RegExp("(" + k + ")").test(fmt))
                        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                return fmt;
  //Call the function to convert the time obtained from VAR times = new date (time stamp) to standard time
  //Calling mode
    console.log(format('yyyy-MM-dd hh:mm:ss', times))
    console.log(format('yyyy-MM-dd', times))
    console.log(format('yyyy/MM/dd', times))
    console.log(format('yyyy year MM month dd day', times))

Second: convert standard time to time stamp
var nowData = new Date();
There are three ways to turn a standard time into a timestamp
The differences are as follows:
var time1 = nowData.getTime();
var time2 = nowData.valueOf();
var time3 = Date.parse(nowData);
The first two are accurate to milliseconds, the second to seconds
//1532868592176
//1532868592176
//1532868592000

The data returned to me in the background during the development of the project is accurate to seconds, but it does not return the following three zeros, which should be added to the front-end processing

Reference article: https://segmentfault.com/a/1190000000481753

Posted by turtlefox on Sun, 26 Jan 2020 08:28:11 -0800