SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Date format function

    I was wondering if one of you guys can direct me to a simple and clean Date formating function a function that would check the value in a text field and format it correctly. If the end user types in 1/1/04, 1-1-04
    I would like to format that to (mm/dd/yyyy) 01/01/2004 and if they type in 1999, 66/44/2004, I want to return a simple alert telling them to enter a correct date format. Any help is appreicated.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cant do this tonight but I just put a calender on my site that will do it for you

    http://www.vicsjavascripts.org.uk/Me...moCalender.htm

    I will do above tomorrow if you do not get any other responces

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    with no other responces

    PHP Code:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd"
    >

    <
    html>

    <
    head>
      <
    title></title>
    </
    head>

    <
    body>
    <
    script language="JavaScript" type="text/javascript">
    <!--
    vic=0;
    document.write('<style> .debug {VISIBILITY: visible; POSITION: absolute; TOP: 500px; z-Index:100; }</style>')
    <!---->
    </script>
    <div class=debug>
     <form name="Show">
     <input type="text" size="10" name="Show1" value="Show1">
     <input type="text" size="10" name="Show2" value="Show2">
     <input type="text" size="10" name="Show3" value="Show3">
     <input type="text" size="10" name="Show4" value="Show4">
     <input type="text" size="10" name="Show5" value="Show5">
     <input type="text" size="10" name="Show6" value="Show6">
     </form>
    </div>

    I was wondering if one of you guys can direct me to a simple and clean Date formating function a function<br>
    that would check the value in a text field and format it correctly. If the end user types in 1/1/04, 1-1-04<br>
    I would like to format that to (mm/dd/yyyy) 01/01/2004 and if they type in 1999, 66/44/2004,<br>
    I want to return a simple alert telling them to enter a correct date format.<br>
    Any help is appreicated. <br>
    <br>
    Solution<br>
    Simple but may not be clean as I just made it<br>
    Cannot separate American and European Format<br>
    Alert text is subjective.<br>
    I dont understand 'and if they type in 1999,'<br>
    but there is an alert for year limits<br>
    <br>
    My calender is far more fun!
    <br>

    <input onblur="javascript:CkDate(this);" >

    <script language="JavaScript" type="text/javascript">
    <!--
    var day,month,year;
    var sep1S,sep1F,sep2S,sep2F;

    function CkDate(tb){
     sep1S=-1; sep1F=0;
     sep2S=-1; sep2F=0;
     // find the first separater
     for (i0=0;i0<tb.value.length;i0++){
      if (isNaN(tb.value.charAt(i0))||tb.value.charAt(i0)==' '){ sep1S=i0; i0=tb.value.length+1; }
     }
     if (sep1S<0){ alert('Date must numbers and separaters'); tb.value='Please ReEnter'; return; }
     if (sep1S<1){ alert('The first chactor of the date must be a number'); tb.value='Please ReEnter'; return; }
     if (tb.value.substring(0,sep1S)>31){ alert('The day/month is incorrect'); tb.value='Please ReEnter'; return; }
     for (i1=sep1S;i1<tb.value.length;i1++){ if (!isNaN(tb.value.charAt(i1))&&tb.value.charAt(i1)!=' '){ sep1F=i1; i1=tb.value.length+1; } }
     if (sep1F<sep1S+1){ alert('Incomplete date'); tb.value='Please ReEnter'; return; }

    // find the second separater
     for (i2=sep1F;i2<tb.value.length;i2++){ if (isNaN(tb.value.charAt(i2))||tb.value.charAt(i2)==' '){ sep2S=i2; i2=tb.value.length+1; } }
     if (sep2S<0){ alert('Incomplete Date'); tb.value='Please ReEnter'; return; }
     for (i3=sep2S;i3<tb.value.length;i3++){ if (!isNaN(tb.value.charAt(i3))&&tb.value.charAt(i3)!=' '){ sep2F=i3; i3=tb.value.length+1; } }
     if (sep2F<sep2S+1){ alert('Incomplete date'); tb.value='Please ReEnter'; return; }
     if (sep2F>tb.value.length-4){ alert('Incorrect Year'); tb.value=tb.value.substring(0,sep2F); return; }
     if (tb.value.substring(sep1F,sep2S)>31){ alert('The day/month is incorrect'); tb.value='Please ReEnter'; return; }
    // format the date
     day=tb.value.substring(0,sep1S);
     if (day.length<2){ day=0+day; }
     month=tb.value.substring(sep1F,sep2S);
     if (month.length<2){ month=0+month; }
     year=tb.value.substring(sep2F,tb.value.length);
     if (year<0){ alert('Please Check the Date'); }
     tb.value=day+'/'+month+'/'+year;
    }

    //-->
    </script>


    </body>

    </html> 

  4. #4
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vwphillips,

    thanks for the function. it works fine in my forms.


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
  •