Thanks in advance...

My question is.. I have one text box there user types the date as follows (d-m-yy)

5-4-09 or 15-3-94

how can i convert them into exact (dd-mm-yyyy)

05-04-2009 or 15-03-1994

is there any 'javascript' code to convert the given date into my desired formt...

please help me...

Hi,

try this one:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>www.daniweb.com</title>
<script type="text/javascript">
<!--
var d = document;
var validate = function( field ) {
var today = new Date();
var field = ( typeof( field ) === "object" ) ? field : d.getElementById( field );
   if ( field && field.value.length ) {
   var hold = [];
   var stamp = [ ( today.getMonth() + 1 ), today.getDate(), today.getFullYear() ];
   var date = field.value.split("-");
      for ( var i = 0; i < date.length; i++ ) {
         var num = Number( date[ i ] );
         hold[ i ] = num;
         hold[ i ] = (( hold[ i ] > stamp[ i ] || !hold[ i ] ) ? stamp[ i ] : hold[ i ] );
         hold[ i ] = (( hold[ i ] < 10 ) ? "0" : "" ) + hold[ i ]; 
      } (( hold[ 2 ] && String( hold[ 2 ] ).length < 4 ) ? hold[ 2 ] = stamp[ 2 ] : void( 0 ));
   field.value = hold.join("-");
   } 
};

// --> 
</script>
</head>
<body>
<div>
<form action="#" id="testform">
<div>Date Format: <em>mm-dd-yyyy</em><br>
<input type="text" id="datefield" name="datefield" size="10" maxlength="10" value="" onchange="validate( this );">
</div>
</form>
</div> 
</body>
</html>

TanQ for your reply...

I tried it for some date like ... 8-6-94 but it gives 08-06-2009

12-5-86 it gives 08-06-2009 even it is not today date also...

but any way tanQ for your contribution...

i got a way from your code...

i will implement it.. and post it..

TanQ very much..

Hi,

i made it to react, that if you provide year lower than a usual 4 digit format, then it will automatically converted to the current year. The format must be on this entry 08-06-1994 to able for that to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.