Timestamp conversion date format

Timestamp conversion date format

What is a timestamp

Unix timestamp (Unix timestamp), or Unix time (Unix time), POSIX time (POSIX time), is a way of time representation, defined as 00:00:00 on January 1, 1970, Greenwich Mean Time The total number of seconds since the present.

The difference between 10-bit time stamp and 13-bit time stamp

The default precision of date is milliseconds, which means that the generated timestamp is 13 digits, and some timestamps are 10 digits by default because the precision is seconds.

Timestamp conversion date format

// 时间戳转换
    formatLinuxTime(data){
        if (data) {
            let time = this.timestampToTime(data);
            return time
        }
        return '';
    },
    timestampToTime(timestamp) {
      var date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
      Y = date.getFullYear() + '-';
      M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
      D = date.getDate() < 10 ? '0' + (date.getDate()) + ' ' : date.getDate() + ' ';
      h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
      m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
      s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
      return Y + M + D + h + m + s;
    },
    console.log(this.formatLinuxTime(1615361100000))

Insert picture description here
Note that the value passed in the formatLinuxTime method must be of type number, and the string will be converted to NaN-NaN-NaN NaN:NaN:NaN

Time is converted to timestamp

  1. js method to get the current timestamp
var timestamp1 = Date.parse(new Date());
var timestamp2 = (new Date()).valueOf();
var timestamp3 = new Date().getTime();
  1. js method to obtain and formulate timestamp
var oldTime = (new Date("2021/03/10 15:25:00")).getTime()
//返回数值的单位是毫秒。

gmt to time

transDate(data) {
      if (data) {
        function renderTime(date) {
          var dateee = new Date(date).toJSON();
          return new Date(+new Date(dateee) + 8 * 3600 * 1000)
            .toISOString()
            .replace(/T/g, " ")
            .replace(/\.[\d]{3}Z/, "");
        }
        let time = renderTime(data);
        return time;
      }
      return "";
    },

Guess you like

Origin blog.csdn.net/weixin_43881166/article/details/114637828