0
   {"date":"Thu Dec 06 14:56:01 IST 2012"}

I am getting this string as JSON can I convert it to JS date object?

2
  • @ADC I have no idea how this is related to the question. Commented Dec 6, 2012 at 9:52
  • @DeepakKaithwas This kind of date format might be a bit difficult to parse. Why would server send such date format? Can you change it to ISO string for example? Commented Dec 6, 2012 at 9:54

2 Answers 2

4

Edit: Unfortunately i was totally wrong, sry for that,my bad, it happened to always result in today, but to not screw you up, heres an solution which should work for you anyway If you get Different Time strings from your Server, maybe the best way is to write a Regex pattern that matches your String patterns

  • Access your date propertie from your JSON Object
  • Since instantiating a Date object with this "Thu Dec 06 14:56:01 IST 2012" String would result in an Invalid Date
  • Remove the "IST" myJson.date.replace(" IST","")
  • Instantiate the your Date object with your new String myDate = new Date("Thu Dec 06 14:56:01 2012")
  • Now theres really your Date Object

var myJson = {"date":"Thu Dec 06 14:56:01 IST 2012"}
var myDate = new Date(myJson.date.replace(" IST",""))
console.log(myDate.toLocaleDateString())

Heres the JSBin

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, it does actually work under Chrome ( missed the inner Date call ). Very interesting.
2

The right way to convert your JSON to the data object it's parsing this date as a string.

var myJson = {"date":"Thu Dec 06 14:56:01 IST 2013"}
var myDate = new Date(Date(myJson.date))
console.log(myDate.getFullYear()) // 2012

Doesn't work with a Year different from the current one.

Related link
Where can I find documentation on formatting a date in JavaScript?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.