View Full Version : Date rewrite .. (simple change of a string)

03-23-2008, 02:39 PM
hello NG .. i have a Date in a Var
returned by a subfunction.
like this:
return MochiKit.DateTime.toISODate(time).substr(2);

the output is simply YY-MM-DD
i would like to rewrite the date to be like DD.MM.YY

but my code :

var day2 = MochiKit.DateTime.toISODate(time).substr(8);
var month2 = MochiKit.DateTime.toISODate(time).substr(5).substring(3);
var year2 = MochiKit.DateTime.toISODate(time).substr(2).substring(6);
return day2 + '.' + month2 + '.' + year2;
(sure somethings very stupid) does not work at all ;-)
the output is always DD.DD.DD

any ideas ??

03-23-2008, 03:56 PM
var str='YY-MM-DD';
var d1=str.substr(6);
var m1=str.substr(3,2);
var y1=str.substr(0,2);

Philip M
03-23-2008, 04:00 PM
The syntax of the string.substr() method requires two arguments. The first is the start position of the substring and the second is the length of the substring to be returned.

yy ="08-03-23"
var day2 = yy.substr(6,2);
var month2 = yy.substr(3,2);
var year2 = yy.substr(0,2)
var date = day2 + '.' + month2 + '.' + year2;
alert (date);

abduraooft beat me to it! :)

03-23-2008, 04:54 PM
works perfectly ...

thanks alot .. well i still not sure what i did wrong .. but the output counts ;-)

03-23-2008, 04:54 PM
The syntax of the string.substr() method requires two arguments. I had the same in mind, but http://www.w3schools.com/jsref/jsref_substr.asp says length is optional.

Philip M
03-23-2008, 05:01 PM
If the length parameter is omitted, the substr() method extracts to the end of the string.

So your

var d1=str.substr(6);
is the same as my
var day2 = yy.substr(6,2);

But for myself I would prefer to make the code explicit.

Here's another method:-

<script type = "text/javascript">

var date = "08/03/24"
var sliced = date.split('/');
//sliced[0] = sliced[0].substr(2,2); // if desired to reduce 4-digit year to 2 digits
var newdate = sliced[2] + ':' + sliced[1] + ':' + sliced[0]; // change order and separator to suit
alert (newdate);