Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-11-2006, 12:24 AM   PM User | #1
nlkflynn
New to the CF scene

 
Join Date: Jul 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
nlkflynn is an unknown quantity at this point
Formatting two digit years.

Formatting two digit years.
Hello,
I ‘m developing an app and one of the requirements is that dates that are entered with two digit years should be formatted to four digit years and it be in this century, so 01/01/01 would become 01/01/2001. I tried the getFullYear() function but it defaults 01 to 1901. Is there something else I can use?

Thanks
nlkflynn is offline   Reply With Quote
Old 07-11-2006, 07:24 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Try adding 100 to 1901 to get 2001.

var now = new Date();
var yynow = now.getYear(); // IE returns 2006. Netscape and Opera return 106.

Last edited by Philip M; 07-11-2006 at 07:28 AM..
Philip M is online now   Reply With Quote
Old 07-11-2006, 11:33 AM   PM User | #3
nlkflynn
New to the CF scene

 
Join Date: Jul 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
nlkflynn is an unknown quantity at this point
Thanks,
I'm not sure that will work, I probably didn't explain myself too clearly in my post. The user will be able to enter any date, so it could be a 2 digit year or 4 digit year. If it's a four digit year we are leaving it be. I guess the problem I'm having is how to know when they've entered a two digit or four digit year. In my code
I'm just creating a date object and then getting the month, day, year from that.

Thanks

Last edited by nlkflynn; 07-11-2006 at 11:39 AM..
nlkflynn is offline   Reply With Quote
Old 07-11-2006, 12:17 PM   PM User | #4
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,357
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Select Lists are the only way to be sure but
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
  <title></title>
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/

function CkYear(obj){
 var val =obj.value.split('/');
 if (val.length!=3){ alert('incorrect format'); return; }
 for (var zxc0=0;zxc0<val.length;zxc0++){
  if (isNaN(val[zxc0])){
   alert('incorrect format'); return;
  }
 }
 if (val[2].length==2){
  val[2]='20'+val[2];
 }
 if (val[2].length!=4){
  alert('incorrect year format'); return;
 }
 if (val[1]<1||val[1]>12){
   alert('incorrect month format'); return;
 }
 if (val[0]<1||val[0]>zxcDinM(val[2],val[1])){
   alert('incorrect day format'); return;
 }
 obj.value=val.join('/');
}

function zxcDinM(zxcyr,zxcm){
 return new Date(zxcyr,zxcm,1,-1).getDate();
}


/*]]>*/
</script>
</head>

<body>
<input value="01/02/2001" onblur="CkYear(this);" >
</body>

</html>
see also

http://www.vicsjavascripts.org.uk/Fo...endium.htm#f14
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Old 07-11-2006, 12:34 PM   PM User | #5
nlkflynn
New to the CF scene

 
Join Date: Jul 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
nlkflynn is an unknown quantity at this point
Great,
Thanks for all the help
nlkflynn is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:19 PM.


Advertisement
Log in to turn off these ads.