...

View Full Version : DateTime format conversion



michael_1980
07-24-2012, 11:39 AM
Hi All,

I want to convert my date time format from 2012-07-23 15:50:56
this format to 23 July 2012, 3:30 PM



how can I do that using javascript functions?

Philip M
07-24-2012, 03:18 PM
Where does your existing time format come from?

But this topic has been covered very recently in this forum. Have a look at http://www.codingforums.com/showthread.php?t=268280

Customer in UK travel agent shop: - "What month is it now in Australia?"

michael_1980
07-25-2012, 10:39 AM
I want to convert my date time format from 2012-07-23 15:50:56
this format to 23 July 2012, 3:30 PM

Any straightway functions available

Philip M
07-25-2012, 11:06 AM
I want to convert my date time format from 2012-07-23 15:50:56
this format to 23 July 2012, 3:30 PM

Any straightway functions available

It is pointlesss to just repeat yourself. I'll ask you one more time. Where does your existing date format come from? Is it a string value?
Assuming that your date is a string value,


<script type = "text/javascript">

var datestring = "2012-07-23 15:50:56";
var mths = ['January','February','March','April','May','June','July','August','September','October','November',' December'];
var ampm = "AM"
datestring = datestring.replace(/[^\d]/gi,"~"); // replace everything not a digit by a ~
var dsplit = datestring.split("~"); // and split the string at ~ into an array
var dt = Number(dsplit[2]);
var mth = dsplit[1]-1 // months in Javascript are 0-11
var yr = dsplit[0];
var hrs = Number(dsplit[3]);
if (hrs > 11) {ampm = "PM"}
if (hrs > 12) {hrs = hrs -12}
var mins = Number(dsplit[4]);
if (mins <10) {mins = "0" + mins}
var newdate = dt + " " + mths[mth] + " " + yr + ", " + hrs + ":" + mins + " " + ampm;

alert (newdate);

</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum