Biribu
Biribu

Reputation: 3823

Comparing date with date in string javascript

I am trying to compare a couple of dates in javascript. First of it, I got froma database. In database it is in date format but after send it in a json file I guess it is just a string.

The second I got it in Date format in javascript. I found this script in javascript:

var todayAEJ = new Date();
var ddAEJ = todayAEJ.getDate();
var mmAEJ = todayAEJ.getMonth()+1; //January is 0!
var yyyyAEJ = todayAEJ.getFullYear();
if(ddAEJ<10){ddAEJ='0'+ddAEJ} if(mmAEJ<10){mmAEJ='0'+mmAEJ} todayAEJ = ddAEJ+'-'+mmAEJ+'-'+yyyyAEJ; 

ANd it works like a charm.

The other date is like this: 13-01-2014

I tried to compare like this:

if(todayAEJ > val.date_End)...

But it returns true when today's day is bigger than val.date_End's day. So I cannot use this form when the month is diferent. What can I do in this case?

Upvotes: 0

Views: 103

Answers (4)

Anubhav
Anubhav

Reputation: 1625

   otherDate = '13-01-2014';
   myDate=myDate.split("-");
   var newDate=myDate[1]+","+myDate[0]+","+myDate[2];
    otherDateTimeStamp = new Date(newDate).getTime();
    todayAEJTimeStamp = new Date().getTime();
    if(todayAEJTimeStamp > otherDateTimeStammp){
      // your code
    }

you can even use var newDate=myDate[1]+"/"+myDate[0]+"/"+myDate[2];

Use above code...it will solve your problem!

Iinjoy...

Upvotes: 1

Marcin Olichwirowicz
Marcin Olichwirowicz

Reputation: 890

Apart from timestamp solutions, I would recommend to use date.js in any case you want to play with Date object in JS. Date object has pretty odd behaviour in JS, and this library comes in handy.

Upvotes: 0

naoxink
naoxink

Reputation: 597

You have to compare them via microtime

var date = new Date("11/21/1987 16:00:00");
var milliseconds = date.getTime(); // Compare this milliseconds

Upvotes: 0

Alex
Alex

Reputation: 11245

If you can use MM-DD-YYYY format instead of DD-MM-YYYY you can use Date.parse:

var todayAEJTimeStamp = Date.parse(todayAEJ);//Date parse needs `MM-DD-YYYY` format

//if val.date_End is instance of Date
if(todayAEJTimeStamp > val.date_End.getTime()) {
    //your code here
}

Upvotes: 0

Related Questions