How to add minutes and hours to a time string using jquery
Asked Answered
A

4

5

I want to add 30 minutes and then one hour to my variable which i already have my own date

var initialDate = '10:00';

So

if (some condition){
    // i add 30 minutes ->10:30
}elseif(another condition){
    // i add 1hour ->11:00
}

I tried this but doesn't work

var initialDate = '10:00';
var theAdd = new Date(initialDate);
var finalDate = theAdd.setMinutes(theAdd.getMinutes() + 30);
Arica answered 21/3, 2017 at 11:34 Comment(6)
So what have you tried? Check Date class developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Formication
@BastianVoigt Please respect the "Be Nice" policy. It was downvoted because, similar to questions asking for software/libraries/etc., answers recommending them aren't very good either.Ranson
Please, Look my updateArica
I think the question how to deal with dates in Javascript is perfectly valid, and so is an answer that recommends a library that does the job. Where's the problem?Constrained
@Bastian Voigt the problem is when i do an alert to finalDate i have NaN , That's why I asked the question I thought this is not the right method.Arica
i tried to add parsInt in var initialDate = parseInt('10:00'); but i have this 1800010 and it's not goodArica
E
17

If I understand you correctly, the following will help you.

You need to add momentjs dependency via script tag and you can Parse, validate, manipulate, and display dates in JavaScript.

You can find more documentation regarding this in momentjs website

console.log(moment.utc('10:00','hh:mm').add(1,'hour').format('hh:mm'));

console.log(moment.utc('10:00','hh:mm').add(30,'minutes').format('hh:mm'));
<script src="https://momentjs.com/downloads/moment-with-locales.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Efrem answered 21/3, 2017 at 11:59 Comment(1)
This adds unnecessary loading time and baggage if they only need to use dates a few times; otherwise, this is a good idea.Ranson
A
5
var theAdd = new Date();

// Set Hours, minutes, secons and miliseconds
theAdd.setHours(10, 00, 00, 000);

if (some condition) {
   // add 30 minutes --> 10:30
   theAdd.setMinutes(theAdd.getMinutes() + 30);
}
elseif (some condition) {
   // add 1 hour --> 11:00
   theAdd.setHours(theAdd.getHours() + 1);
}

Then you print the var theAdd to obtain the date and time.

To obtain just the time:

theAdd.getHours() + ":" + theAdd.getMinutes();
Apperception answered 21/3, 2017 at 12:14 Comment(0)
R
1

This should do the job. Dates need a year and month in their constructor, and you have to specify larger units of time if you specify and smaller ones, so it needs a day as well. Also, you have to pass in the hours and minutes separately. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date.

var initialDate = '10:00';
var theAdd = new Date(1900,0,1,initialDate.split(":")[0],initialDate.split(":")[1]);
if(30 min condition){
theAdd.setMinutes(theAdd.getMinutes() + 30);
} else if (1 hour condition){
theAdd.setHours(theAdd.getHours() + 1);
}
console.log(theAdd.getHours()+":"+theAdd.getMinutes());
Ranson answered 21/3, 2017 at 12:16 Comment(1)
Your solution solve my problem. Thank you a lotDdt
P
0

Here is a javascript function that will add minutes to hh:mm time string.

function addMinutes(timeString, addMinutes) {
if (!timeString.match(/^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/))
    return null;
var timeSplit = timeString.split(':');
var hours = parseInt(timeSplit[0]);
var minutes = parseInt(timeSplit[1]) + parseInt(addMinutes);
hours += Math.floor(minutes / 60);
while (hours >= 24) {
    hours -= 24;
}
minutes = minutes % 60;
return ('0' + hours).slice(-2) + ':' + ('0' +minutes).slice(-2);

}

Preterhuman answered 21/7, 2017 at 13:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.