Forum Moderators: open
I am easily able to get this working for the US date formats - MM/DD/YYYY
But I am unable to get the datediff to work out the difference between 2 UK format dates - DD/MM/YYYY
The code that I am currently using is:
var strDate1 = document.formName.fieldName.value
var strDate2 = document.formName.fieldName.value
datDate1= Date.parse(strDate1);
datDate2= Date.parse(strDate2);
dateDiff = ((datDate1-datDate2)/(24*60*60*1000))
I have tried formatting the date to a UK date but this just comes back with a value of either "undefined" or "NaN".
Any help is much appreciated
Thanks
UK-format (dd/mm/yyyy) dates could be problematic. JScript [msdn.microsoft.com], for eg, expects mm/dd/yyyy (with forward slashes too).
Maybe there's a better way, but if not, here's a static method for the Date constructor that makes dates using dd/mm/yyyy format.
Date.fromUKFormat = function(sUK)
{
var A = sUK.split(/[\\\/]/);
A = [A[1],A[0],A[2]];
return new Date(Date.parse(A.join('/')));
}wrongToday = new Date(Date.parse("25/1/2005"))
today = Date.fromUKFormat("25/1/2005");alert("bad: " +wrongToday)
alert("good: "+today)
Which keeps on throwing back the incorrect dateDiff. The main reason why I am using javascript is so that it does it on the fly so when you press submit it checks it there and then.
If there is any other way to do this it would be very much appreciated.
this works out the date diff for a maximum of 31 days and if the date diff is above 31 days it will show an alert.
If you want to change the datediff days then just change the 31 to the number of days you want.
function dateDifference()
{
var strDate1 = document.FrmName.DateStart.value
var strDate2 = document.FrmName.DateEnd.value
var bldateDiff = false
//Start date split to UK date format and add 31 days for maximum dateDiff
strDate1 = strDate1.split("/");
starttime = new Date(strDate1[2],strDate1[1]-1,strDate1[0]);
starttime = new Date(starttime.valueOf()+31*86400000);
//End date split to UK date format
strDate2 = strDate2.split("/");
endtime = new Date(strDate2[2],strDate2[1]-1,strDate2[0]);
endtime = new Date(endtime.valueOf());
if(endtime > starttime)
{
bldateDiff = true
}
return bldateDiff
}
------------------------------------------------------
function submit()
//This will go into the submit button function.
bldateDiff = dateDifference()
// Takes the date from the Start date and adds
// 31 days to make it the maximum end date
strDate1 = strDate1.split("/");
starttime = new Date(strDate1[2],strDate1[1]-1,strDate1[0]);
starttime = new Date(starttime.valueOf()+31*86400000);
starttime = padDigit(starttime.getDate())+"/"+padDigit(starttime.getMonth()+1)+"/"+starttime.getYear();
// if bldateDiff = true than the end date is later than 31 days.
if (bldateDiff==true)
{
alert("Please pick an End Date before:\n\n" + starttime)
}
else
{
if (blSubmit==true)
{frmSearchFlds.submit();}
}
------------------------------------------------------
<input type="text" name="StartDate">
<input type="text" name="EndDate">
<input type="submit" onclick="Submit()"
------------------------------------------------------
I know it is a bit lengthy but this is my first script so it could do with a tidy up I think.