Comparing a date to a date/time in a client script

Uncle Rob
Kilo Patron

We have a catalog item that asks a client for a date (start_date). My stakeholders want to warn the user not to pick a start_date prior to today. I can find out what start_date is, and I can find out what "rightnow" is, but I run into trouble comparing them, since rightnow is invariably a DateTime

Here's my code so far



function onChange(control, oldValue, newValue, isLoading) {
var reqDate = g_form.getValue('start_date');
// var reqDate = newValue;
alert ('reqdate ' + reqDate);
var dateParts = reqDate.split('-');
alert ('dateParts ' + dateParts);
var jsCompareDate1 = new Date(dateParts[0], dateParts[1] - 1, dateParts[2]);
alert ('compare1 ' + jsCompareDate1);
var jsNowDate = new Date();
var jsCompareDate2 = new Date(jsNowDate.getFullYear(), jsNowDate.getMonth(), jsNowDate.getDate());

if (jsCompareDate1 <= jsCompareDate2) {
alert ("Date: " + jsCompareDate1 + "is before date: " + jsCompareDate2);
} else {
alert ("Date: " + jsCompareDate1 + "is after date: " + jsCompareDate2);
}
}


The real problem seems to be that dateParts doesn't find numbers with the split. It doesn't evaluate "06", but rather "Jun", and therefore can't come up with anything valid in jsCompareDate1.

I could also be tackling this in the fashion of a raving mad lunatic.

Is there any hope for me?

2 REPLIES 2

tony_fugere
Mega Guru



function onChange(control, oldValue, newValue, isLoading) {
if(isLoading || newValue == '') {
return;
}

var reqDate = new Date(getDateFromFormat(newValue, g_user_date_format));
var today = new Date();

if(dateReq.getTime() < today.getTime()) {
alert('Start date must not be in the past.');
g_form.setValue('start_date','');
}
}


Uncle Rob
Kilo Patron

Tony,

I owe you a beer next time you're in the People's Republic of Chicago.