React-js converts time strings to timestamps for time comparison

1. When we need to compare time, we get the time in string form, which needs to be compared with the time format. At this time, we need to convert both forms of time into a timestamp for comparison.
I don't know how the specific timestamp is defined, but there is a sentence in Baidu Encyclopedia: "The timestamp is the total number of seconds from January 1, 1970 (00:00:00 GMT) to the current time".

According to this definition, there is a similar function in programming languages, getTime(), but this function returns the total number of milliseconds from January 1, 1970 to the current time, not the sum.

2. The following is my method call

这段代码的目的是将传入时间(字符串形式)与今天日期进行比较,如果大于今天,则传入今天时间,不然则传入传入日期,进行比较的都是已经转换为时间戳形式
  // 时间格式转换 
  dataFormat(date) {
    
    
  //  today: Date.now(), // 今天日期 
    const {
    
     today } = this.state; // 今天时间戳
    const todayTime = moment().format('YYYY-MM-DD'); // 今天字符串
    const dateFormat = date.replace(/-/g, '/'); // 将 - 转为 /  // 将-替换成/,因为下面这个构造函数只支持/分隔的日期字符串
    const newDateFormat = Date.parse(dateFormat); // 转为时间戳形式
    const newDate = newDateFormat > today ? todayTime : date;  // 得到字符串
    return newDate;
  }

Guess you like

Origin blog.csdn.net/weixin_45416217/article/details/107093517