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

date - Javascript check if it has passed midnight since a certain time - Stack Overflow

programmeradmin1浏览0评论

I need to create a javascript function that checks if it has been a day since timeX (an instance of Date). I do NOT mean whether is has been 24 hours since timeX, but instead whether it has passed a midnight since timeX.

I am a PHP expert, not a JavaScript one, so I was wondering if anyone here had any quick answers. Thanks!

function(dateLast, dateNow) {...}

I need to create a javascript function that checks if it has been a day since timeX (an instance of Date). I do NOT mean whether is has been 24 hours since timeX, but instead whether it has passed a midnight since timeX.

I am a PHP expert, not a JavaScript one, so I was wondering if anyone here had any quick answers. Thanks!

function(dateLast, dateNow) {...}
Share Improve this question asked Apr 16, 2010 at 4:47 JonahJonah 2,0807 gold badges29 silver badges32 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

A simple solution to check whether two dates represent the same day is:

function isSameDay(a, b) {
    return a.toDateString() == b.toDateString();
}

This works no matter what the distance between the two dates is.

if(dateNow.getDate() == dateLast.getDate()){
 //it's the same day
}else{
 //another day, so midnight has passed
}

EDIT: changed the condition, now it should work.

发布评论

评论列表(0)

  1. 暂无评论