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

Comparing C# MinDate with javascript min date failing - Stack Overflow

programmeradmin1浏览0评论

When my sproc returns a null, it goes into a C# DateTime as a MinDate. If a date of this form gets to the front end (MVC/Razor), I create a "MinDate" type of my own in JavaScript for parison. If they match, I want to return an empty string from the JavaScript function. However, although the two dates match, the branch of my "if" statement that returns the empty string is never entered. I've looked at these two dates until I'm cross eyed and they appear to match. Why is my function returning the MinDate and not the empty string? (By the way, the "toString()" method is from the DateJS library.)

// Setup a minDate to mimic C#'s Date.MinDate constant.
var minDate = new Date();
minDate.setFullYear(1, 0, 1);
minDate.setHours(0, 0, 0, 0);

function checkDateWithConfig(d, c) {

    alert("Date: " + d);
    alert("minDate: " + minDate);

    if (d == minDate) {
        alert("dates matched");
        return "";
    }
    else
    {
        return d.toString(c);
    }
}

When my sproc returns a null, it goes into a C# DateTime as a MinDate. If a date of this form gets to the front end (MVC/Razor), I create a "MinDate" type of my own in JavaScript for parison. If they match, I want to return an empty string from the JavaScript function. However, although the two dates match, the branch of my "if" statement that returns the empty string is never entered. I've looked at these two dates until I'm cross eyed and they appear to match. Why is my function returning the MinDate and not the empty string? (By the way, the "toString()" method is from the DateJS library.)

// Setup a minDate to mimic C#'s Date.MinDate constant.
var minDate = new Date();
minDate.setFullYear(1, 0, 1);
minDate.setHours(0, 0, 0, 0);

function checkDateWithConfig(d, c) {

    alert("Date: " + d);
    alert("minDate: " + minDate);

    if (d == minDate) {
        alert("dates matched");
        return "";
    }
    else
    {
        return d.toString(c);
    }
}

Share Improve this question asked Aug 1, 2012 at 14:05 birdusbirdus 7,52417 gold badges64 silver badges95 bronze badges 1
  • Could you also please output diff of the dates var diff = d.getTime()-minDate.getTime(); – George Mamaladze Commented Aug 1, 2012 at 14:12
Add a ment  | 

1 Answer 1

Reset to default 6

Javascript Date objects are references. You cannot use == to check whether two Date objects represent the same value.

Instead, pare the getTime() method on both dates, which returns a number.

发布评论

评论列表(0)

  1. 暂无评论