计算当前日期和日期之间的差异,并从差异值开始计算时间



我有一个包含数据的网格。我执行加载网格((函数以在网格完全加载之前加载数据或条件。

当atction状态等于1时,我打算在这一行中通过当前日期与对象日期(date.key.date(的差值启动计时器。

也就是说,如果相差 30 分钟...我希望计时器从 00:30:00 开始计数......

我的问题是计时器没有启动,我无法将其值传递给 fetchdisplay (( 服务的功能;

有人可以帮助我吗?

演示

法典

data = [
{
id: 1,
idUser: 1,
name: "Name1",
active: 1,
date: "2020-02-01T20:00:00",
},
{
id: 2,
idUser: 2,
name: "Name2",
active: 0,
date: "2020-02-01T20:00:00",
},
{
id: 3,
idUser: 3,
name: "Name3",
active: 0,
date: "2020-02-01T20:00:00",
}
];

loadgrid(event) {
if (event != null) {
if (event.rowType != "header") {
if (event.data.decorrer == 1){
this.interval = setInterval(() => {
this.display = +this.datePipe.transform(Date.now(), 'yyyyMMddHHmmss'); - +this.datePipe.transform(event.data.date, 'yyyyMMddHHmmss');
// this.taskService.fetchDisplay() = this.display;
return this.display;
}, 1000);
}
}
}
}
startTimer(data) {
alert("start timer");
this.currentRowIndex = data.id;
let self = this;
self.taskService.startTimer(data);
self.currentState = self.taskService.getCurrentState();
}
pauseTimer(data) {
alert("pause timer");
let self = this;
this.currentRowIndex = data.id;
self.taskService.pauseTimer(data);
self.currentState = self.taskService.getCurrentState();
}

您的问题中缺少一些信息,但您通常会通过每秒更新计数来setInterval做到这一点,这将获得当前日期和给定日期之间的差异。在您的情况下,给定的日期可能如下所示Date.parse(data[0].date)

data = [
{
id: 1,
idUser: 1,
name: "Name1",
active: 1,
date: "2020-02-01T20:00:00",
}
]

const date = Date.parse(data[0].date);  //the date of the object
const elem = document.getElementById('counter')
function count() {
let countFrom = (Date.now() - date);	//get the difference with the current date

//convert to seconds, minutes and hours
seconds = parseInt((countFrom/1000) % 60)
minutes = parseInt((countFrom/(1000*60)) % 60)
hours = parseInt((countFrom/(1000*60*60)) % 24);
//append `0` infront if a single digit
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
	
let time = `${hours}:${minutes}:${seconds}`;
elem.textContent = time;

}
setInterval(count, 1000);
<div id='counter'></div>