PDA

View Full Version : date format into uk



Skinnybobb
04-24-2006, 02:03 PM
Hi,
I'm wanting to output the stored varaible in to english date format.
E.g. 12/03/2005 (american format) into 03/12/2005 (UK 3rd Dec 2005)
My code so far is:

function calculation(form) {
var from = form.fromDate.value;
var to = form.toDate.value;
var num1 = form.numDays.value;
var date1 = new Date(from);
var date2 = new Date(to);
var one_day = 1000*60*60*24;

//Calculate difference btw the two dates, and convert to days
form.numDays.value = (Math.ceil((date2.getTime()-date1.getTime() + one_day)/(one_day)));

I know this is v simple but I cant see the wood for the trees!
Any Help would be great.
Mark

jscheuer1
04-24-2006, 02:43 PM
Without seeing your markup or knowing what will later be done (the entire script) with the date, if anything, it is very hard to say. Consider this:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title>Reform Date - Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function reformDate(dateString){
var dateAr=dateString.split('/')
return dateAr[1]+'/'+dateAr[0]+'/'+dateAr[2]
}
</script>
</head>
<body>
<input type="text" value="05/25/62" onclick="this.value=reformDate(this.value)">
</body>
</html>

Please post a link to the page on your site that contains the problematic script so we can check it out.

Skinnybobb
04-24-2006, 03:00 PM
Hiya John,
My full code is:


<head>
<title></title>

<script language="javascript">

function calculation(form) {
var from = form.fromDate.value;
var to = form.toDate.value;
var num1 = form.numDays.value;
var date1 = new Date(from);
var date2 = new Date(to);
var one_day = 1000*60*60*24;

//Calculate difference btw the two dates, and convert to days
form.numDays.value = (Math.ceil((date2.getTime()-date1.getTime() + one_day)/(one_day)));

}
</script>
</head>

<body>
<form name="myForm">
<table border="1" width="100%">
<tr>
<td width="25%">from</td>
<td width="25%">to</td>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%"> <input type="text" name="fromDate">
</td>
<td width="25%"> <input type="text" name="toDate">
</td>
<td width="25%"> <input type="text" name="numDays">
</td>
<td width="25%">&nbsp;</td>
</tr>
</table>
<br>
<INPUT TYPE="button" VALUE="Calculate" onClick="calculation(this.form);" class="sp2_btn">
<INPUT TYPE="reset" class="sp2_btn">
</from>


</body>

</html>


When the calculate button is clicked the number of milliseconds is calculated between the fromDate and the toDate this is then written to the text field called numDays.
At the moment it calculates according to the american format and not the uk format that I need!

No website available. Sorry.
Mark

jscheuer1
04-24-2006, 05:09 PM
I already answered your question then, you just didn't see it yet:


<script type="text/javascript">

function reformDate(dateString){
var dateAr=dateString.split('/')
return dateAr[1]+'/'+dateAr[0]+'/'+dateAr[2]
}

function calculation(form) {
var from = reformDate(form.fromDate.value);
var to = reformDate(form.toDate.value);
var num1 = form.numDays.value;
var date1 = new Date(from);
var date2 = new Date(to);
var one_day = 1000*60*60*24;

//Calculate difference btw the two dates, and convert to days
form.numDays.value = (Math.ceil((date2.getTime()-date1.getTime() + one_day)/(one_day)));

}
</script>

Notes: It might be a good idea to provide a button, checkbox or radio buttons that toggle(s) between using US and UK date formats for input. The language attribute for the script tag has been deprecated, use the type attribute instead. You had a typo in your example HTML markup that could cause problems in certain situations (just before the closing </body> tag):


</from>

Skinnybobb
04-25-2006, 11:45 AM
Thank you very much John.

I was being a bit of a numpty yesterday.
You are, as always, the master! Enough brown nosing.

Cheers again
Mark