I have json datetime like this 2013-11-09T00:00:00 I want to extract the date from this string using Jquery or javascript ?
2 Answers
The winner is:
var date = new Date( jsonDate .replace("T"," ") .replace(/-/g,"/") ); since that is the only one that works in IE AND FX AND Chrome
var jsonDate = "2013-11-09T00:00:00"; var date = new Date(Date.parse(jsonDate)); console.log("With parse\t\t"+date); date = new Date(jsonDate); console.log("Without parse\t\t"+date); date = new Date(jsonDate.replace("T", " ")); console.log("Without T\t\t"+date); date = new Date(jsonDate.replace("T", " ").replace(/-/g, "/")); console.log("Without T and dash\t"+date); var dayNames = ["Sun", "Mon", "Tues", "Wed", "Thu", "Fri", "Sat"], monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], pad = function(str) { return ("0" + str).slice(-2); } function formatDateStr(dStr) { var date = new Date(dStr.replace("T", " ").replace(/-/g, "/")); // Sat Nov 09 2013 return dayNames[date.getDay()] + " " + monthNames[date.getMonth()] + " " + pad(date.getDate()) + " " + date.getFullYear(); } console.log(formatDateStr("2013-11-09T00:00:00")); var jsonDate = "2013-11-09T00:00:00"; 1AM in Chrome, NaN in IE8, OK in Fx:
var date = new Date(Date.parse(jsonDate)); window.console&&console.log("With parse "+date); 1AM in Chrome, NaN in IE8, OK in Fx:
var date = new Date(jsonDate) window.console&&console.log("Without parse "+date); OK in Chrome, NaN in IE8, Invalid Date in Fx:
date = new Date(jsonDate.replace("T"," ")); window.console&&console.log("Without T "+date); OK in Chrome, OK in IE8, OK in Fx:
date = new Date(jsonDate.replace("T"," ").replace(/-/g,"/")); window.console&&console.log("Without T and dash "+date); UPDATE - formatting:
In Chrome and Fx you may get away with .split(" ") and take the first 4 entries, but in IE you will have Sat Nov 9 00:00:00 UTC+0100 2013 - and toLocaleString gives 11/9/2013 12:00:00 AM on my box
var dayNames = ["Sun","Mon","Tues","Wed","Thu","Fri","Sat"], monthNames= ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"], pad=function(str) {return ("0"+str).slice(-2);} function formatDateStr(dStr) { var date = new Date(dStr.replace("T"," ").replace(/-/g,"/")); // Sat Nov 09 2013 return dayNames[date.getDay()]+ " "+monthNames[date.getMonth()]+ " "+pad(date.getDate())+ " "+date.getFullYear(); } window.console&&console.log(formatDateStr("2013-11-09T00:00:00")); 7 Comments
Saturnix
why are you replacing the "T"?
mplungjan
You are right.... I think I got confused with something I did a while ago, let me check
Prateek
@mplungjan now its perfect.
mplungjan
I was not confused. I needed to remove T AND the dash
|
var d = "2013-11-09T00:00:00"; new Date(Date.parse(d)); 4 Comments
mplungjan
Test in more than one browser
Louis Ricci
@mplungjan - works in the latest FF and chrome, could you be less cryptic?
mplungjan
try IE - still a few million IE8 users out there. The Date.Parse is not necessary for the browsers that support the date format, the - dash kills IE. Just read my answer to see what I meant
Louis Ricci
@mplungjan - IE8 kudos!