Saturday 22 July 2017

Best Practices for representing Times in Node

(I'm still getting my head around some things here, so sorry for the simple questions.) What is the best recommended way internal to your application to pass dates and times around?Currently I'm loading from the data store, converting into a Moment object in the UTC Timezone inside the DAO, and passing this Moment object around. And this works fine, but it's a bit clunky at the unit testing layer - I can't trivially just compare two objects that happen to contain a Moment and expect it to work.Is it better to instead pass around a Date() object - given that Javascript doesn't do timezones properly? Or to pass around a unix timestamp (seconds or millis since the epoch) or an ISO-8601 String? All of these would work better for unit tests, and then I can do the conversions for timezones and formatting at the very last moment. I'm currently leaning towards the ISO-8601 string, but I'm not sure what is the generally recommended best way to handle this?For reference - I'm talking about what data type to store these values in inside the model objects that are used in between the DAO and the HTTP layer, and these model objects travel in both directions. Inside the database it's just a TIMESTAMPTZ that is used, always in UTC, and over the HTTP API it's always an ISO-8601 String, but on occasion in between the two I need to be able to compare and manipulate the values.

Submitted July 22, 2017 at 06:54PM by sazzer

No comments:

Post a Comment