WraithNath
WraithNath

Reputation: 18013

Whats wrong with my Javascript Date Comparison

Im tyring to compare two dates in javascript but im getting strange values when calculating the dates.

Can anyone see anything obvious in my code that is causing the issue?

The problem is the Todays date variable looks like a normal date, but my calculation for next week and last week are coming out as large numbers and the comparison wont work.

//Handles client side date selection changed
function dateSelectionChanged(sender, args) {

    //Declare array for Day names
    var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

    //Get the date
    var date = sender.get_selectedDate();

    //Get todays Date
    var today = new Date();
    var nextWeek = new Date().setDate(today.getDate() + 7);
    var lastWeek = new Date().setDate(today.getDate() - 7);

    //Show the day name
    $('#<%= txtDay.ClientID %>').val(days[date.getDay()]);

    if ( date < lastWeek ) {
        alert('Date Under Week');
    }
    if ( date > nextWeek ) {
        alert('Date Over Week');
    }
}

And here is the Code in Debug so you can see the values: enter image description here

EDIT: Solution

//Handles client side date selection changed
function dateSelectionChanged(sender, args) {

    //Declare array for Day names
    var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

    //Get the date
    var date = sender.get_selectedDate();

    //Get todays Date
    var today = new Date();
    var nextWeek = new Date().setDate(today.getDate() + 7);
    var lastWeek = new Date().setDate(today.getDate() - 7);

    //Get the dates in easier to compare format
    nextWeek = new Date(nextWeek);
    lastWeek = new Date(lastWeek);

    //Show the day name
    $('#<%= txtDay.ClientID %>').val(days[date.getDay()]);

    if ( date < lastWeek) {
        alert('Date Under Week');
    }
    if ( date > nextWeek) {
        alert('Date Over Week');
    }
}

Upvotes: 1

Views: 1405

Answers (3)

codeandcloud
codeandcloud

Reputation: 55298

Another method is to set dates like this.

var today = new Date();
var nextWeek = new Date();
nextWeek.setDate(today.getDate() + 7);
var lastWeek = new Date();
lastWeek.setDate(today.getDate() - 7);

Upvotes: 0

Sangeet Menon
Sangeet Menon

Reputation: 9915

If you supply no arguments, the constructor creates a Date object for today's date and time according to local time. If you supply some arguments but not others, the missing arguments are set to 0. If you supply any arguments, you must supply at least the year, month, and day. You can omit the hours, minutes, seconds, and milliseconds.

The date is measured in milliseconds since midnight 01 January, 1970 UTC. A day holds 86,400,000 milliseconds. The Date object range is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC.

try the following code while comparing the dates

if ( today < lastWeek.getMilliseconds() ) {
        alert('Date Under Week');
    }
    if ( today > nextWeek.getMilliseconds() ) {
        alert('Date Over Week');
    }

The Date object provides uniform behavior across platforms.

Upvotes: -1

pimvdb
pimvdb

Reputation: 154968

For setDate, JavaScript returns timestamps, which represent the amount of milliseconds since 1 January 1970 00:00:00 to a specific moment in time. It might look useless, but in fact is very useful as you can represent any time as a simple number.

In case you want to get back a Date, you can use:

new Date(timestamp);

so e.g. add:

nextWeek = new Date(nextWeek);
lastWeek = new Date(lastWeek);

Upvotes: 2

Related Questions