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

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

문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top