简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM