I have my string which contains time like var time = '11:50 AM'
I try to add 10 minutes to my string, so it should be like 12:00 PM, the both minutes and (AM/PM) has to be changed.
How to perform this kind of operations? In JavaScript.
as I'm new to the technology, please help me out.
Here's one way to do it in a few easy to read steps.
However, working with dates in vanilla JavaScript can be tricky. It might be worth looking into a library like Moment.js which makes working with dates/times easier.
// string
var timeString = '11:50';
// create proper date with the string
var date = new Date('2022-01-01T' + timeString+ ':00Z');
// add 10 minutes
var newDate = new Date(date.getTime() + 10*60000);
// split the new string
var newDateSplit = newDate.toTimeString().split(':');
// get hour and minutes from split date
var newTimeString = newDateSplit[0] + ':' + newDateSplit[1];
console.log('original time: ' + timeString)
console.log('new time: ' + newTimeString)