admin管理员组

文章数量:1194791

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);

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);
Share Improve this question edited Mar 21, 2017 at 14:09 SAMUEL 8,5523 gold badges45 silver badges43 bronze badges asked Mar 21, 2017 at 11:34 princeprince 1231 gold badge1 silver badge10 bronze badges 6
  • So what have you tried? Check Date class developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… – Justinas Commented Mar 21, 2017 at 11:35
  • 1 @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. – Feathercrown Commented Mar 21, 2017 at 11:41
  • Please, Look my update – prince Commented Mar 21, 2017 at 11:45
  • 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? – Bastian Voigt Commented Mar 21, 2017 at 11:47
  • @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. – prince Commented Mar 21, 2017 at 11:56
 |  Show 1 more comment

4 Answers 4

Reset to default 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>

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();

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());

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);

}

本文标签: javascriptHow to add minutes and hours to a time string using jqueryStack Overflow