Results 1 to 5 of 5

Thread: date format into uk

  1. #1
    Join Date
    Dec 2005
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Red face date format into uk

    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:
    Code:
    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

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    <!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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Dec 2005
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default date format

    Hiya John,
    My full code is:
    Code:
    <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

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I already answered your question then, you just didn't see it yet:

    Code:
    <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):

    HTML Code:
    </from>
    Last edited by jscheuer1; 04-24-2006 at 05:21 PM.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Dec 2005
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •