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

How to get next 12:00:00PM in javascript date object - Stack Overflow

programmeradmin1浏览0评论

Just need to know how to get the closest 12:00:00pm in the JavaScript date object, for some reason I'm baffled! EG if it is 09:00AM on the 1st of July, then it will be 12:00PM 1st July, however if it's 01:00PM on the 1st of July, then I need 12:00PM 2nd July returning.

Cheers.

Just need to know how to get the closest 12:00:00pm in the JavaScript date object, for some reason I'm baffled! EG if it is 09:00AM on the 1st of July, then it will be 12:00PM 1st July, however if it's 01:00PM on the 1st of July, then I need 12:00PM 2nd July returning.

Cheers.

Share Improve this question asked Aug 4, 2011 at 8:09 rickyduckrickyduck 4,08414 gold badges61 silver badges95 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 6

Like this: Add a day if hours > 12

var nextNoon = new Date();
if (nextNoon.getHours() >= 12) nextNoon.setDate(nextNoon.getDate() + 1)
nextNoon.setHours(12, 0, 0, 0)
console.log(nextNoon)

try this ...

var dt = new Date();
var tomorrowNoon = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate() + 1, 12, 0, 0);

I've checked it out for going past the end of the month and that works too ...

var dt = new Date(2011, 7, 31);
var tomorrowNoon = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate() + 1, 12, 0, 0);

JavaScript's Date is lenient in the sense that e.g. Aug 32 equals Sep 1, so something like this perhaps:

function getNextNoon() {
  var noon = new Date();
  if (noon.getHours() >= 12) {
    noon.setDate(noon.getDate() + 1);
  }
  noon.setHours(12);
  noon.setMinutes(0);
  return noon;
}
发布评论

评论列表(0)

  1. 暂无评论