-1

I am trying to build an if statement on javascript;

if (nextProcessingDate > today ) {
  //do something
}

nextProcessingDate is assigned a timestamp, i.e. 09/07/2014 12:10:17

I am using the following code to assign today's timestamp to today variable

/*script start*/

    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth() + 1; //January is 0!
    var yyyy = today.getFullYear();

    var s = today.getSeconds();
    var m = today.getMinutes();
    var h = today.getHours();

    if (dd < 10) {
        dd = '0' + dd
    }

    if (mm < 10) {
        mm = '0' + mm
    }
    if (s < 10) {
        s = '0' + s
    }
    if (m < 10) {
        m = '0' + m
    }
    if (h < 10) {
        h = '0' + h
    }

    today = dd + '/' + mm + '/' + yyyy + ' ' + h + ':' + m + ':' + s;

/*script end*/

Now what I really want to do is "If nextprocessingDate is bigger than today plus One day then do something"

 if (nextProcessingDate > (today + 1){
   //do something
}

Thanks for your help, here is the fiddle link http://jsfiddle.net/E5vG4/1/

Update So, here is the updated fiddle which seems to work to some extend. http://jsfiddle.net/E5vG4/9/

The issue now is that it only returns true of the processingdate timestamp is equals or over 24hours from now, I need to test it for 09:30am which in this case it returns false.

Would stripping the time from the next processing date fix it somehow?

David Garcia
  • 3,056
  • 18
  • 55
  • 90

4 Answers4

1

You definitely should not be using strings to compare dates.

You could try creating a Date object from nextProcessingDate then:

var nextProcessingDate  = '09/07/2014 12:10:17';
var d = Date.parse(nextProcessingDate);
if (d > (new Date().getTime() + (1000 * 60 * 60 * 24))) {
       alert("after");
} else {
       alert("before");
}
rob
  • 1,286
  • 11
  • 12
1

Date processing is a pain in JavaScript and writing your own solution is all sorts of tricky once you factor in periods crossing midnight, daylight saving changes and leap days.

I recommend selecting an established date processing library to do this kind of stuff.

I've been using moment.js and have found it to be pretty great.

Your code would become:

if(moment(nextProcessingDate).isAfter(moment(today).add('days',1))) {
  // do something
}

or, more succinctly:

if(moment('09/07/2014 14:05:17').isAfter(moment().add('days',1))) {
  // do something
}
Dancrumb
  • 26,597
  • 10
  • 74
  • 130
1
today.setDate(today.getDate() + 1);

before var dd = today.getDate(); can make your Date appear one day after today.

Updated Fiddle

Kawinesh S K
  • 3,148
  • 1
  • 16
  • 29
  • The issue now is that it only returns true of the processingdate timestamp is equals or over 24hours from now, I need to test it for 09:30am which in this case it returns false, I guess because is not a full day from now? – David Garcia Jul 09 '14 at 13:10
1
 if (nextProcessingDate > (today + 1){
   //do something
}

Will just add a 1 after the seconds.

If you just want to check if an excisting timestamp is bigger than todays date + 1 day you should just do like what you did with the month. So if the day i 09, dd will become 10 insted.

So if I understood your question right, try this:

var dd = today.getDate() + 1;
Norrj
  • 114
  • 6