Reputation:
I have a start time to which i would like to add an end time to. for example
startTime=19:09
endTime=00:51 // 0 hours and 51 minutes
i want to add the 51 minutes to the 19:09 to make it 20:00.
I have tried multiple different scenarios as showing bellow but nothing is giving me the correct time
i tried
let [hour, minute] = endTime.split(':').map(Number);
this.endTime = Moment(startTime)).add({ hour: 'hours', minute: 'minutes' }) // also tried .add(hour,'hours').add(minute,'minutes')
which still outputs 19:09. its just ignoring my end time
i tried
Moment(endTime, 'hh:mm').add(Moment.duration(startTime)).format("hh:mm");
which gives me an output of 08:00 when it should be 20:00
What am i doing wrong? i want to add the end time to a start time. Keep in mind that my endTime is always changing so sometimes it could be 13:05 etc cause its a user input
Upvotes: 1
Views: 5041
Reputation: 17382
There are three major issues with your code:
Creating a moment with a timestamp alone (ie something like moment('19:09')
without a date) like you do is deprecated and throws an error. You either have to pass in a fully specified timestamp in RFC2822 or ISO format or explicitely tell the library, what input format you are using.
The object you are passing to the add()
function literally is
{
hour: "hours",
minute: "minutes"
}
ie, instead of passing the numerical values for hours and minutes to add to your
moment, you are passing the strings "hours"
and "minutes"
, which obviously
momentsjs can't handle.
The format hh:mm
only accepts hours from 0
to 12
. If you want a 24-hour clock you have to use HH:mm
Taking these issues into account, the following snippet works as expected:
let start = '2021-01-07 19:09',
duration = '0:51',
starttime = '19:09';
let [hour, minute] = duration.split(":");
//shorthand initialization for the argument
let endtime1 = moment(start).add({hour, minute}).toString();
//explicit definition of property names
let endtime2 = moment(start).add({hours: hour, minutes: minute}).toString();
//add hours and minutes separately
let endtime3 = moment(start).add(hour, "hours").add(minute, "minutes").toString();
//provide a format for the timestamp. momentsjs will take the current date for the date value
let endtime4 = moment(starttime, "HH:mm").add(hour, "hours").add(minute, "minutes").toString();
console.log(endtime1);
console.log(endtime2);
console.log(endtime3);
console.log(endtime4);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Also keep in mind, that for specifiying which part of the timestamp to manipulate, you can either use singular or plural wording. Ie
moment(...).add(4, "hour").add(17, "minute")
and moment(...).add({hour: 4, minute: 17})
is equivalent to
moment(...).add(4, "hours").add(17, "minutes")
and moment(...).add({hours: 4, minutes: 17})
respectively as can also be seen in the snippet with the creation of endtime1
and endtime2
Upvotes: 1
Reputation: 802
You need to convert your duration into a single unit as minutes
, seconds
, days
etc...
Then you can use the following snippet to add duration.
you can uses moment
methods to convert your duration
const mins = moment.duration(10, "hour").asMinutes();
const someTime = moment('19:09',"HH:mm");
const data = someTime.add('51','minutes').format("HH:mm")
//More clever solution would be
const data2 = someTime.add(1, "hours").add(51, "minutes").format("HH:mm")
console.log(data)
console.log(data2)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Upvotes: 1