893 B
893 B
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | |
|---|---|---|---|---|---|---|
| Add minutes to date | snippet | javascript |
|
dreamy-flowers | 2020-11-28T19:27:46+02:00 |
Calculates the date of n minutes from the given date, returning its string representation.
- Use the
Dateconstructor to create aDateobject from the first argument. - Use
Date.prototype.getTime()andDate.prototype.setTime()to addnminutes to the given date. - Use
Date.prototype.toISOString(),String.prototype.split()andString.prototype.replace()to return a string inyyyy-mm-dd HH:MM:SSformat.
const addMinutesToDate = (date, n) => {
const d = new Date(date);
d.setTime(d.getTime() + n * 60000);
return d.toISOString().split('.')[0].replace('T',' ');
};
addMinutesToDate('2020-10-19 12:00:00', 10); // '2020-10-19 12:10:00'
addMinutesToDate('2020-10-19', -10); // '2020-10-18 23:50:00'