We have a requirement, where we want to use a consistent date time format across server side code & on mobile client javascript code. We decided to go with moment.js, for date formatting on client side. However the outputs of the two are not in sync.
Time Format String: "**YYYY-MM-DD'T'HH:mm:ss.SSSZZ**"
Java Code & Output
java.util.Date d2 = new java.util.Date();
SimpeDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD'T'HH:mm:ss.SSSZZ");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
String formattedDate = sdf.format(d2);
System.out.println(formattedDate);
Output:2015-02-06T03:28:24.333+0000
Moment JS Output
var d2 = new Date();
var format = "YYYY-MM-DD'T'HH:mm:ss.SSSZZ";
alert(moment(d2).format(format));
Output: 2015-02-06'T'03:28:24.333+0000
With moment.js, T is coming in quotes. Similary, if the format is " YYYY-MM-DD'TXYZ'HH:mm:ss.SSSZZ ". Java output: 2015-02-06TXYZ03:28:24.333+0000, Moment output: 2015-02-06'T1423193658Y+05:30'09:04:18.351+0530
Can someone suggest me a solution, or a better alternate.
var format = "YYYY-MM-DD'T'HH:mm:ss.SSSZ"
Try leaving off the quotes around 'T' when you set the format.
您可以使用矩和格式使用与Java兼容的ZonedDateTime:
moment(this.date).format('YYYY-MM-DDTHH:mm:ss:SSZ')
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.