Comparing a date to a date/time in a client script
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-06-2012 01:25 PM
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

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-07-2012 07:06 AM
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','');
}
}
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-07-2012 07:33 AM
Tony,
I owe you a beer next time you're in the People's Republic of Chicago.