Using JavaScript, How can I subtract from HH:MM:SS?
使用JavaScript,我如何减去HH:MM:SS?
For example, I have 12:54:45 and I want to subtract 11:35:53 for remaining time in 4732 seconds
例如,我有12:54:45而且我想在4732秒内减去剩余时间的11:35:53
3 个解决方案
#1
3
var first = (new Date('Jan 01, 2000 12:54:45')).getTime();
var second = (new Date('Jan 01, 2000 11:35:53')).getTime();
var differenceInSeconds = (second - first) / 1000;
// 4732
I chose 2000-01-01
as the date, as it does not matter as long it is the same. We only want to calculate the difference.
我选择2000-01-01作为日期,因为它并不重要,只要它是相同的。我们只想计算差异。
#2
1
This works
(Date.parse('January 1, 1970 12:54:45') - Date.parse('January 1, 1970 11:35:53')) / 1000
Output is 4732
输出为4732
#3
1
There are libraries that makes possible to manipulate dates in an easy way: moment.js and day.js are two examples of choice.
有些库可以轻松地操作日期:moment.js和day.js是两个选择的例子。
// moment.js example
moment()
.add(7, 'days')
.subtract(1, 'months')
.year(2009)
.hours(0)
.minutes(0)
.seconds(0);
// day.js example
dayjs().add(7, 'day');
Also, for seconds manipulation, Unix time can be useful. It allows to describe time as the number of seconds elapsed since 1st January 1970.
此外,对于秒操作,Unix时间可能很有用。它允许将时间描述为自1970年1月1日以来经过的秒数。
#1
3
var first = (new Date('Jan 01, 2000 12:54:45')).getTime();
var second = (new Date('Jan 01, 2000 11:35:53')).getTime();
var differenceInSeconds = (second - first) / 1000;
// 4732
I chose 2000-01-01
as the date, as it does not matter as long it is the same. We only want to calculate the difference.
我选择2000-01-01作为日期,因为它并不重要,只要它是相同的。我们只想计算差异。
#2
1
This works
(Date.parse('January 1, 1970 12:54:45') - Date.parse('January 1, 1970 11:35:53')) / 1000
Output is 4732
输出为4732
#3
1
There are libraries that makes possible to manipulate dates in an easy way: moment.js and day.js are two examples of choice.
有些库可以轻松地操作日期:moment.js和day.js是两个选择的例子。
// moment.js example
moment()
.add(7, 'days')
.subtract(1, 'months')
.year(2009)
.hours(0)
.minutes(0)
.seconds(0);
// day.js example
dayjs().add(7, 'day');
Also, for seconds manipulation, Unix time can be useful. It allows to describe time as the number of seconds elapsed since 1st January 1970.
此外,对于秒操作,Unix时间可能很有用。它允许将时间描述为自1970年1月1日以来经过的秒数。