最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

adding 30 minutes to time using javascript - not working - Stack Overflow

programmeradmin2浏览0评论

need some help with adding extra time to current time

currentTime= new Date(); 
var hours2 = currentTime.getHours() + 4;
var minutes2 = currentTime.setMinutes(currentTime.getMinutes() + 30);

however it outputs as: 15:1455880112692PM and does not seem to be adding 4.5 hours? anyone have any ideas on this?

need some help with adding extra time to current time

currentTime= new Date(); 
var hours2 = currentTime.getHours() + 4;
var minutes2 = currentTime.setMinutes(currentTime.getMinutes() + 30);

however it outputs as: 15:1455880112692PM and does not seem to be adding 4.5 hours? anyone have any ideas on this?

Share Improve this question asked Sep 17, 2012 at 10:10 James BrandonJames Brandon 1,4003 gold badges19 silver badges43 bronze badges 3
  • Post outdated please see here > stackoverflow./questions/1197928/… – TheMonkeyMan Commented Sep 17, 2012 at 10:12
  • Ive already looked at those examples, which are the same as mine but output is different – James Brandon Commented Sep 17, 2012 at 10:13
  • and you should be expecting your result stored in currentTime. Is it? – Rab Commented Sep 17, 2012 at 10:18
Add a ment  | 

4 Answers 4

Reset to default 2

That's because you are not getting the minutes, you are getting the Date object. The setMinutes method doesn't return the minutes, it returns the Date object itself. Converting a Date object to a string to display it gives you the time in milliseconds since epoch.

First convert the time, so that you get a correct time that wraps over, and not something like 27:93 instead of 04:33.

currentTime = new Date();
currentTime.setHours(currentTime.getHours() + 4);
currentTime.setMinutes(currentTime.getMinutes() + 30);

Then you can get the hours and minutes from it:

var hours2 = currentTime.getHours();
var minutes2 = currentTime.getMinutes();

Instead of adding 4 hours and 30 minutes, you can add 270 minutes:

currentTime = new Date();
currentTime.setMinutes(currentTime.getMinutes() + 270);

I have a few ideas as to what's wrong:

currentTime= new Date(); 
var hours2 = currentTime.getHours() + 4;//doesn't call the setHours method
var minutes2 = currentTime.setMinutes(currentTime.getMinutes() + 30);//works fine for me

So

currentTime= new Date(); 
var hours2 = currentTime.setHours(currentTime.getHours() + 4);//doesn't call the setHours method
var minutes2 = currentTime.setMinutes(currentTime.getMinutes() + 30);//works fine for me

Does work fine, however, you could just do this in a one-liner:

currentTime.setMinutes(currentTime.getMinutes() + 270);
//or in seconds:
currentTime.setSeconds(currentTime.getSeconds() + 270*60);//16200 === 4.5 hrs
//or miliseconds:
currentTime.setTime(currentTime.getTime() + 270*60000);//16200000 === 4.5 hrs

You never set the hours. Try:

currentTime= new Date(); 
currentTime.setHours(currentTime.getHours() + 4);
currentTime.setMinutes(currentTime.getMinutes() + 30);

Try this I really dont see what the problem you are having is? http://jsfiddle/H9edz/

currentTime= new Date(); 
var hours2 = currentTime.getHours() + 4;
var minutes2 = currentTime.setMinutes(currentTime.getMinutes() + 30);
document.write("Ending Time: " + currentTime);
document.write("<br/>");
document.write("Ending Minute: " + currentTime.getMinutes());
document.write("<br/>");
var now = new Date();
now.setMinutes(now.getMinutes() + 30);
document.write("Example Time - From Post:" + now)

发布评论

评论列表(0)

  1. 暂无评论