How to convert the front-end timestamp into a date, how to convert a moment into a timestamp, and calculate the difference in days based on the timestamp?

1. How to convert timestamp to time

  //时间戳转为日期
  const timechange = (timestamp: any) => {
    
    
    let date = new Date(timestamp * 1000);
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    //时分秒以此类推
    return Y + M + D;
  };

2. Moment is converted to a date, and the date is converted to a timestamp

 let end = moment(date?.valueOf()).format('YYYY-MM-DD');//也可以到时分秒
 //日期转时间戳
 let etime = moment(end).unix();

3. Calculate the difference in days based on the timestamp

let daysDifference = Math.floor((etime - stime) / (24 * 3600)); // 相差的天数

// Calculate the number of milliseconds remaining after the number of days
var leave1 = (etime - stime)% (24 * 3600 * 1000);
if you do not need days as the unit, directly use the difference of the timestamp to divide the time unit (hour, minute and second):
hour : 3600 1000
minutes: 60
1000
seconds: 1000

Guess you like

Origin blog.csdn.net/qq_37967853/article/details/129615340