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:

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');
}
}
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:so e.g. add: