Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format Date fields after submit 1

Status
Not open for further replies.

apex82

Programmer
Mar 2, 2009
127
GB
Hi,

I have a two date fields that accept the following type of dates:

"01/01/1922" or "01-Jan-1922" or "010122"

Once the form is submitted I would like to display the date in one format.

So if a user enters 010132 for one field and 02-Feb-1942 for another both would be displayed as the same format.

i.e.
01/01/1932

02/02/1942

I’ve googled this but can’t find anything.

Can anyone help?

Thanks.


 
Hi

apex82 said:
I've googled this but can't find anything.
Also searched here on Tek-Tips ? Because the first part of your question is answered in your previous thread216-1551786 .

Have you tried to solve the second part based on the first ?

Feherke.
 
Have you tried to solve the second part based on the first ?

Yes, I've spent a long time trying to figure out how to implement this but have been unable. Really not having any luck with it.
 
Hi

Code:
formatteddate=anydate.replace(
  /^(\d{2})[./-]?(\d{2}|\w{3})[./-]?(\d{2})?(\d{2})$/,
  function(p0,p1,p2,p3,p4) {
    var month=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    for (var i=0;i<12;i++) if (p2==month[i]) { p2=(i<9?'0':'')+(i+1); break }
    return p1+'/'+p2+'/'+(p3?p3:'19')+p4
  }
)


Feherke.
 
Thanks ferherke

I’m still try to implement this!

Do I put the code you posted above in a function and then call the function as below?

Code:
<input name="DateOfBirth" type="text" id="DateOfBirth" class="mainnav" tabindex="5" onChange="checkform()" onblur="datecheck(document.getElementById('DateOfBirth'))">

onChange="checkform()" is used for another check the onblur would be used to call the function to check the date.

Thanks for your help.
 
Hi

HTML:
<input name="DateOfBirth" type="text" id="DateOfBirth" onChange="checkform()" onblur="datecheck(this)">
JavaScript:
function datecheck(what)
{
  what.value=what.value.replace(
    /^(\d{2})[./-]?(\d{2}|\w{3})[./-]?(\d{2})?(\d{2})$/,
    function(p0,p1,p2,p3,p4) {
      var month=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
      for (var i=0;i<12;i++) if (p2==month[i]) { p2=(i<9?'0':'')+(i+1); break }
      return p1+'/'+p2+'/'+(p3?p3:'19')+p4
    }
  )
}

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top