Javascript JS new Date() format in different browsers

So if you ever use the command “new Date();” this is what you will get from various browsers

 

Google Chrome:
Thu May 03 2012 12:40:00 GMT-0500 (Central Daylight Time)

Firefox:
Thu May 03 2012 12:40:00 GMT-0500 (Central Daylight Time)

Safari:
Thu May 03 2012 12:40:00 GMT-0500 (Central Daylight Time)

 

Internet Explorer 9:
Thu May 3 12:40:00 CDT 2012

 

Go figure… Everyone but Microsoft uses the same date format. There format doesn’t even make sense, why is the year after the time?

 

A workaround for IE so all browsers will instead produce Microsoft SQL datetime format

<script>

var nowin = new Date();
document.write(mssqlDateTime(nowin));

 

function mssqlDateTime(now){
var monthlookup=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var hours=now.getHours();
if(hours>12){
hours-=12;
ampm=’PM’;
}else{
ampm=’AM’;
}
var safedate = monthlookup[now.getMonth()] + ” ” + now.getDate() + ” ” + now.getFullYear() + ” ” + hours + “:” + String(“0″ + now.getMinutes()).slice(-2) + ampm;
return safedate;
}

</script>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>