...

View Full Version : Formatting date and time



howard-moore
07-14-2010, 07:13 PM
Hi All,

I wonder if you can help me with some time and date formatting in JS.

I already use a neat date formatting JS script which is useful when a PERL script returns a date field of 'DATE' by converting it from yyyy-mm-dd to dd month(in text) yyyy:

<script type = "text/javascript">

var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
var myString = "[[date]]";
var mySplitResult = myString.split("-");
var mm = parseInt(mySplitResult[1],10)-1; // note that months are 0-11
document.write(mySplitResult[2] + " " + months[mm] + " " + mySplitResult[0] );

</script>

I have, however, run across a little problem where I have results coming through with date AND time, e.g.

18:12 2010-07-14

What I need is something that will reorder the above to:

14 July 2010 at 18:12

Can someone help please!

Regards,
Neil

Old Pedant
07-14-2010, 08:43 PM
Utterly untested. Off the top of my head:


<script type = "text/javascript">

var months = ["January","February","March","April","May","June","July","August","September","October","November"," December"];
var myString = "[[date]]";
var temp = myString.split(" ");
var theTime = temp[0];
var theDate = temp[1];
var mySplitResult = theDate.split("-");
var mm = parseInt(mySplitResult[1],10)-1; // note that months are 0-11
document.write(mySplitResult[2] + " " + months[mm] + " " + mySplitResult[0]
+ " " + theTime);
</script>

That is *assuming* that the only space in the [[date]] will be the one between the time and the date.

howard-moore
07-15-2010, 10:26 AM
Excellent - works perfectly - thank you very much.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum