Last Updated: February 25, 2016
·
3.023K
· chrisbumgardner

Show node.js dates/times in the user's timezone in a human friendly format (e.g. 'last Friday at 3:52 PM')

The following gist shows how to turn those server-side date objects in node into nice human-readable, relative dates & times for your users: https://gist.github.com/4254862. It also shows how to grab the user's timezone from their browser, pass it to node, store it in the session, and then use it when ready to format dates server-side (for example to render in a Jade template).

Say for example you are showing the date and time a post was created. For users on the US west coast ('America/Los_Angeles') you want them to see 'last Friday at 3:52 PM'. But for users on the US east coast ('America/New_York') you want them to see 'last Friday at 6:52 PM'. And your node server is running on GMT. Problem solved!

Based on the libraries 'jstz' (to detect the Olson-timezone in the browser), and node modules 'moment', 'time', and 'timezone'.

We're using this code with https://askyourusers.com in order to show our customers info about when their responses were submitted, and our microconsultants info about available jobs.