How to handle dates in Backbone?

后端 未结 3 1834
醉话见心
醉话见心 2021-02-14 11:31

I store dates in the DATETIME format in a MySQL database. When a model is fetched from the database, dates (in the DATETIME format) are converted to date objects in the model\'s

3条回答
  •  逝去的感伤
    2021-02-14 12:10

    I would advice using UNIX time (number of seconds/milliseconds from 1970) both in model and in the interface and converting to readable date only in View.

    So the server both sends and receives dates as numbers like 1328281766454 and this is how you store them in Backbone.Model. When it has to be rendered in View you can simply call:

    new Date(this.model.get('someTime'));  //Fri Feb 03 2012 16:09:26 GMT+0100 (CET)
    

    The same can be done on the server side. Believe me, this is the simplest and most portable way of transfrering dates without all these time-zone issues.

提交回复
热议问题