简体   繁体   中英

Generate WCF JSON format Date with moment.js or another javascript library

I am consuming a WCF Service in Javascript and need to play around with dates.

I was looking around for some nice DateTime handlers for the format the DataContractJsonSerializer generates { "date": "/Date(1260597600000-0600)/" } and found moment.js. moment.js is really excellent to consume this date format, handles the format including the timezone.

What I need now is generate the WCF date format from a Javascript or moment Date to send dates with timezones in the request on my POST method and looking in the documentation of moment.js couldn't find anything that has the output I need.

Any idea how to achieve this with moment.js or any other js library?

Thanks.

With moment.js:

yourMomentObject.format("/[Date](xZZ)/")

Example:

截图

Without moment, you can write your own function that takes uses the Date object's .getTime() function, and .getTimezoneOffset() functions. However, the offset has to be negated, then formatted properly before being added to the string.

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