период времени между датами в соответствии с измерением
Функция принимает 3 параметра - дата1 (string) - дата2 (string) - измерение ('days', 'hours', 'minutes', 'seconds'). Не могу понять как сделать правильно. В коде ошибки.
'use strict';
function durationBetweenDates(firstDate, secondDate, dimensionBetween) {
const firstDate = new Date();
const secondDate = new Date();
const millisecondsDiff = secondDate.getTime() - firstDate.getTime();
if (dimensionBetween === 'seconds') {
const secondsType = millisecondsDiff / 1000;
return secondsType;
}
if (dimensionBetween === 'minutes') {
const minutesType = Math.ceil(millisecondsDiff / (1000 * 60));
return minutesType;
}
if (dimensionBetween === 'hours') {
const hoursType = millisecondsDiff / (1000 * 3600);
return hoursType;
}
if (dimensionBetween === 'days') {
const daysType = millisecondsDiff / (1000 * 3600 * 24);
return daysType;
} else {
return console.log('Not Found');
}
return durationBetweenDates;
}
console.log(durationBetweenDates('02 Aug 1985', '03 Aug 1985', 'seconds'));
//return '86400 seconds'
console.log(durationBetweenDates('31 Jan 2022', '03 Feb 2021', 'days'));
//return '362 days'
Ответы (1 шт):
Автор решения: SwaD
→ Ссылка
У вас 2 ошибки в коде.
Первая: Пытались повторно объявить переменные firstDate, secondDate
Вторая: Для создания даты, необходимо передавать в конструктор значение, из которого будет формироваться дата new Date(firstDate)
function durationBetweenDates(firstDate, secondDate, dimensionBetween) {
const calcFirstDate = new Date(firstDate);
const calcSecondDate = new Date(secondDate);
const millisecondsDiff = calcSecondDate.getTime() - calcFirstDate.getTime();
if (dimensionBetween === 'seconds'){
const secondsType = millisecondsDiff / 1000;
return secondsType;
}
if (dimensionBetween === 'minutes'){
const minutesType = Math.ceil(millisecondsDiff / (1000 * 60));
return minutesType;
}
if (dimensionBetween === 'hours'){
const hoursType = millisecondsDiff / (1000 * 3600);
return hoursType;
}
if (dimensionBetween === 'days'){
const daysType = millisecondsDiff / (1000 * 3600 * 24);
return daysType;
} else {
return console.log('Not Found');
}
return durationBetweenDates;
}
console.log(durationBetweenDates('02 Aug 1985', '03 Aug 1985', 'seconds'));
//return '86400 seconds'
console.log(durationBetweenDates('31 Jan 2022', '03 Feb 2021', 'days'));
//return '362 days'
UPD:
Ваша функция в сокращенном варианте написания
function durationBetweenDates(firstDate, secondDate, dimensionBetween) {
const millisecondsDiff = new Date(secondDate).getTime() - new Date(firstDate).getTime();
switch (dimensionBetween) {
case 'seconds': return millisecondsDiff / 1000;
case 'minutes': return Math.ceil(millisecondsDiff / (1000 * 60));
case 'hours': return millisecondsDiff / (1000 * 3600);
case 'days': return millisecondsDiff / (1000 * 3600 * 24);
default: return 'Not Found';
}
}
console.log(durationBetweenDates('02 Aug 1985', '03 Aug 1985', 'seconds'));
//return '86400 seconds'
console.log(durationBetweenDates('31 Jan 2022', '03 Feb 2021', 'days'));
//return '362 days'