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

javascript - Determine if two dates falls between two dates - Stack Overflow

programmeradmin2浏览0评论

I would like to check if two dates falls between another two dates. So for example:

date aStart = 20-10-2013
date aEnd   = 30-10-2013
date bStart = 21-10-2013
date bEnd   = 29-10-2013

bStart and bEnd falls between aStart and aEnd. Currently I have this code, but it fails on some conditions:

if(bannerStartDateTime > bannerEndDateTime) {
    alert("Start Date Banner > End Date Banner.");
    return;
}
if(bannerStartDateTime < workOrderCampaignStartDateTime) {
    alert("Start Date Banner < Start Date WO.");
    return;
}
if(bannerEndDateTime < bannerStartDateTime) {
    alert("End Date Banner < Start Date Banner.");
    return;
}
if(bannerEndDateTime > workorderCampaignEndDateTime) {
    alert("End Date Banner > End Date WO.");
    return;
}

What's wrong with the code? Thanks.

I would like to check if two dates falls between another two dates. So for example:

date aStart = 20-10-2013
date aEnd   = 30-10-2013
date bStart = 21-10-2013
date bEnd   = 29-10-2013

bStart and bEnd falls between aStart and aEnd. Currently I have this code, but it fails on some conditions:

if(bannerStartDateTime > bannerEndDateTime) {
    alert("Start Date Banner > End Date Banner.");
    return;
}
if(bannerStartDateTime < workOrderCampaignStartDateTime) {
    alert("Start Date Banner < Start Date WO.");
    return;
}
if(bannerEndDateTime < bannerStartDateTime) {
    alert("End Date Banner < Start Date Banner.");
    return;
}
if(bannerEndDateTime > workorderCampaignEndDateTime) {
    alert("End Date Banner > End Date WO.");
    return;
}

What's wrong with the code? Thanks.

Share Improve this question edited Jul 18, 2014 at 6:49 jgillich 76.6k7 gold badges60 silver badges88 bronze badges asked Jul 18, 2014 at 6:40 mrjimoy_05mrjimoy_05 3,58810 gold badges61 silver badges98 bronze badges 3
  • 5 How are you creating the dates? Your syntax in the first code block is not valid JS. – Barmar Commented Jul 18, 2014 at 6:43
  • What variable names are you using? The ones at the top are different to the ones at the bottom - meaning that you are paring undefined with undefined. – SAMdroid Commented Jul 18, 2014 at 6:47
  • The variable that you have declared are not used in paring purpose. Also pare date by converting string into date object – Roshan Commented Jul 18, 2014 at 6:49
Add a ment  | 

4 Answers 4

Reset to default 2

What's wrong with the code?

The statement:

date aStart = 20-10-2013

is not valid javascript. And:

if (bannerStartDateTime > bannerEndDateTime)

uses two identifiers that haven't be declared or initialised.

If you mean to convert the string '20-10-2013' to a date, then you need to write a small parser like:

function dmyToDate(s) {
  var b = s.split(/\D+/);
  return new Date(b[2], --b[1], b[0]);
}

Do not be tempted to pass a date string to the Date constructor, as that will call Date.parse which is notoriously inconsistent across browsers, even when using the format specified in ECMA-262.

So now your code can be:

var bannerStartDateTime = dmyToDate('20-10-2013');
var bannerEndDateTime   = dmyToDate('30-10-2013');

and so on. You can then proceed with parisons using the < and > operators.

if (bStart > aStart && bStart < aEnd && bEnd > aStart && bEnd < aEnd) {
    alert ("bStart-bEnd is between aStart-End");
}

You need to create date variables. Note that string format is 'MM/DD/YYYY'.

var aStart = new Date('10/20/2013'),
    aEnd   = new Date('10/30/2013'),
    bStart = new Date('10/21/2013'),
    bEnd   = new Date('10/29/2013');

Now you can pare dates using getTime() method

if (aStart.getTime() < aEnd.getTime()) {
    $('.msgContainer').append('<div>a-ha</div>');
}

http://jsfiddle/LafkE/

You have to use correct date format and date object in javascript.

var aStart = new Date('2013-10-20');
var aEnd   = new Date('2013-10-30');
var bStart = new Date('2013-10-21');
var bEnd   = new Date('2013-10-29');

if ((bStart > aStart && bStart < aEnd) && (bEnd > aStart && bEnd < aEnd)) {
    alert ("bStart and bEnd both are between aStart and aEnd");
}
else if (bStart > aStart && bStart < aEnd) {
    alert ("bStart is between aStart-End");
}
else if (bEnd > aStart && bEnd < aEnd) {
    alert ("bEnd both is between aStart and aEnd");
}

DEMO

发布评论

评论列表(0)

  1. 暂无评论