How do I format a Microsoft JSON date?

后端 未结 30 3114
伪装坚强ぢ
伪装坚强ぢ 2020-11-21 04:48

I\'m taking my first crack at Ajax with jQuery. I\'m getting my data onto my page, but I\'m having some trouble with the JSON data that is returned for Date data types. Basi

30条回答
  •  清歌不尽
    2020-11-21 05:04

    Everyone of these answers has one thing in common: they all store dates as a single value (usually a string).

    Another option is to take advantage of the inherent structure of JSON, and represent a date as list of numbers:

    { "name":"Nick",
      "birthdate":[1968,6,9] }
    

    Of course, you would have to make sure both ends of the conversation agree on the format (year, month, day), and which fields are meant to be dates,... but it has the advantage of completely avoiding the issue of date-to-string conversion. It's all numbers -- no strings at all. Also, using the order: year, month, day also allows proper sorting by date.

    Just thinking outside the box here -- a JSON date doesn't have to be stored as a string.

    Another bonus to doing it this way is that you can easily (and efficiently) select all records for a given year or month by leveraging the way CouchDB handles queries on array values.

提交回复
热议问题