admin管理员组

文章数量:1277334

I need to pare two date in javascript.

In my code I am calling a web service method which serialize and returns in JSON format an instance of the following class

public class MyPeriod
{
    public int PeriodID { get; set; }
    public DateTime BeginDate { get; set; }
    public DateTime FirstDeadline { get; set; }
    public DateTime SecondDeadline { get; set; }
    public DateTime EndDate { get; set; }
} 

this will produces a JSON fragment like this:

{
    "d": {
        "PeriodID":26,
        "BeginDate":"\/Date(1321743660000)\/",
        "FirstDeadline":"\/Date(1322002860000)\/",
        "SecondDeadline":"\/Date(1322002860000)\/",
        "EndDate":"\/Date(1322168400000)\/"
    }
}

Now I need to pare FirstDeadline with SecondDeadline and so in my javascript I have a fragment like this

var date1 = eval(data.d.FirstDeadline.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
var date2 = eval(data.d.SecondDeadline.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
if (date1 === date2) {
    ...
}

Unfortunately the equal parison does not work even if the dates have the same value. Any help?

UPDATE 1

I forgot to mention that I need to pare only the date part (that's way I am not paring the milliseconds since the epoch).

I need to pare two date in javascript.

In my code I am calling a web service method which serialize and returns in JSON format an instance of the following class

public class MyPeriod
{
    public int PeriodID { get; set; }
    public DateTime BeginDate { get; set; }
    public DateTime FirstDeadline { get; set; }
    public DateTime SecondDeadline { get; set; }
    public DateTime EndDate { get; set; }
} 

this will produces a JSON fragment like this:

{
    "d": {
        "PeriodID":26,
        "BeginDate":"\/Date(1321743660000)\/",
        "FirstDeadline":"\/Date(1322002860000)\/",
        "SecondDeadline":"\/Date(1322002860000)\/",
        "EndDate":"\/Date(1322168400000)\/"
    }
}

Now I need to pare FirstDeadline with SecondDeadline and so in my javascript I have a fragment like this

var date1 = eval(data.d.FirstDeadline.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
var date2 = eval(data.d.SecondDeadline.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
if (date1 === date2) {
    ...
}

Unfortunately the equal parison does not work even if the dates have the same value. Any help?

UPDATE 1

I forgot to mention that I need to pare only the date part (that's way I am not paring the milliseconds since the epoch).

Share Improve this question edited Nov 23, 2011 at 16:28 Lorenzo asked Nov 23, 2011 at 16:16 LorenzoLorenzo 29.4k50 gold badges128 silver badges224 bronze badges 2
  • I'm not sure why that doesn't work, but you could convert the Date to a string (.toString, .toGMTString, .toUTCString, .toISOString) and use that for parison. Seems to work. – ampersand Commented Nov 23, 2011 at 16:27
  • also, see: stackoverflow./questions/7244513/… – ampersand Commented Nov 23, 2011 at 16:28
Add a ment  | 

7 Answers 7

Reset to default 4

You can do

if(date1.toDateString() === date2.toDateString()) {

Try paring their "time" (milliseconds since the epoch) values:

if (date1.getTime() === date2.getTime()) {
  // OK
}

[Edit]

If you only care about the date and not the time, then something like this will work:

function sameDate(d1, d2) {
  return ((d1.getFullYear() === d2.getFullYear())
       && (d1.getMonth() === d2.getMonth())
       && (d1.getDate() === d2.getDate()));
}
if (sameDate(date1, date2)) {
  // OK
}

Try to use getTime method of Date object for date parison. some thing like this which will pare the time ticks.

if (date1.getTime() == date2.getTime()) {

}

Answer to your edited question

if(date1.getMonth() == date1.getMonth() && 
   date1.getDate() == date2.getDate() &&
   date1.getFullYear() == date2.getFullYear()){

}

First off, your parison if failing because the === operator looks for absolute equality. But you're constructing two different date objects. Comparing date objects is not the same as paring a primitive value, such as an number. So, === will always fail, even if the dates represent the same value.

var a = new Date(100000);
var b = new Date(100000); 
console.log(a === b); // false
console.log(a.getTime() === b.getTime()); // true

var a = new Date(100000);
var a = b;
console.log(a === b); // true

Secondly, if you're grabbing the numeric porition of that value (the epoch value, in milliseconds), you don't actually need to create a new Date object to pare them. Why not just pare the numbers against each other directly?

EDIT: Per your edit, if you're just wanting to pare the "date" part:

 if(date1.getDay() === date2.getDay() && date1.getMonth() === date2.getMonth() && date1.getFullYear() === date2.getFullYear()){ 
 }

First of all, when you have a 'Date(1322002860000)', the numeric portion is the number of milliseconds since epoch.

What you have to do is :

var myDate = new Date();
myDate.setTime(1322002860000);

You can also use a more advanced epoch converter, something like they are doing over there: http://www.esqsoft./javascript_examples/date-to-epoch.htm

Hope this helps!

you should convert them to a javascript Date object like:

var d = new Date();
d.setTime(1321743660000);

then you can pare. See http://www.w3schools./jsref/jsref_obj_date.asp for more information

You can use JSON.parse instead of eval, and use a reviver to construct dates as described here : How to use JSON.parse reviver parameter to parse date string

If you know the JSON is from a trusted source you can change

.replace(/\/Date\((\d+)\)\//gi, "new Date($1)")

to

.replace(/\/Date\((\d+)\)\//gi,
         function (_, n) { return new Date(+n); })

which uses replace with a function replacer to construct a new date object whose millis-since-epoch is the numeric value from group 1.

本文标签: jqueryjavascript Datetime compareStack Overflow