js gets the specified time + timestamp display time minutes and seconds

During the development, I encountered a rather nonsensical request from a customer.
It is obvious that there is an option to operate hours, minutes and seconds. If you don’t want to do it, you will be lazy and save trouble. A time must be displayed as the selected year, month and day, but the time format must be passed to the backend in the format of hours, minutes and seconds. , the list display also takes time, minutes and seconds.
So it was dealt with

 let DATE: any = new Date(); // 当前日期
 let timestamp = Date.parse(DATE);
 let now = new Date(timestamp);
 console.log(timestamp);

Insert image description here

 function getData(n) {
	  let now = new Date(n),
	    y = now.getFullYear(),
	    m = now.getMonth() + 1,
	    d = now.getDate();
	  return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d) + " " + now.toTimeString().substr(0, 8);
}
getData(1691031289000) // 

Insert image description hereThe above encapsulates a simple date conversion, which is enough.
Then there is the processing of date splicing. I found that all we need is hours, minutes and seconds.

The processing is: selected time + " " + selected time.toTimeString().substr(0, 8); This is the time we want.

选中的时间 + " " + 选中的时间.toTimeString().substr(0, 8); 即是我们要的时间。


if ('cgTime' in changedFields) { // 判断是否点击了时间弹框,如果点击了就可以拿到选中的时间,如果没有就设置为空
  let cgTime = ''; // 选中的时间初始为空
  let DATE: any = new Date();
  const timestamp = Date.parse(DATE);
  let now = new Date(timestamp);
  if (changedFields.cgTime) {
    cgTime = changedFields.cgTime + ' ' + now.toTimeString().substr(0, 8);
    console.log(cgTime, 'cgTimecgTimecgTime');
  } else {
    this.formRef.setFieldsValue({
      cgTime: null,
    });
  }
  this.formRef.setFieldsValue({
    cgTime,
  });
}

Supongo que te gusta

Origin blog.csdn.net/lzfengquan/article/details/132078803
Recomendado
Clasificación