How to format Javascript date object to String, coming from a Java GregorianCalendar object, through JSON

StackOverflow https://stackoverflow.com/questions/22611686

Question

I have parsed an object, containing a GregorianCalendar Object, from java, to JSON, and onwards into a Javascript Object. The object I have in Javascript looks like this when printed in console.log()

Object
dayOfMonth: 27
hourOfDay: 0
minute: 0
month: 4
second: 0
year: 2014

Is there a way to format this through something similar to Java's SimpleDateFormat, into a pattern like dd/mm-yy? Or is it a better approach to parse the Calendar-object to a string before turning it into JSON-format in the first place?

Était-ce utile?

La solution

I'd take your second approach (GregorianCalendar.getTime() to your preferred String format) then simply new Date(dateString); within JavaScript once you need the date object. This makes for a smaller transmission. It appears either way will work for you.. just so you aren't losing data.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top