samsonjs / strftime

strftime for JavaScript

Home Page:http://samhuri.net/projects/strftime

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

add strftimeUTC(...)

andrewschaaf opened this issue · comments

commented

Same as strftime, but using d.getUTC{Hours,...}

commented

Here's a hacky approach:

UTCify_date = (x) ->
  y = new Date()
  y.setFullYear     x.getUTCFullYear()
  y.setMonth        x.getUTCMonth()
  y.setDate         x.getUTCDate()
  y.setHours        x.getUTCHours()
  y.setMinutes      x.getUTCMinutes()
  y.setSeconds      x.getUTCSeconds()
  y.setMilliseconds x.getUTCMilliseconds()
  y


exports.strftimeUTC = strftimeUTC = (format, d) ->
  strftime format, UTCify_date(d)

commented

Would this hack be merged if it also special-cased %z and %z?

Seems useful, I would merge this. I was surprised to see there's no way to convert a Date to UTC directly.

It's still a hack but how about instead of setting so many properties we use the TZ offset, e.g.

function toUTC(d) { return new Date(+d - (1000 * 60 * 60 * d.getTimezoneOffset() || 0)) }

Do you see any problems with that method? I have || 0 in case getTimezoneOffset returns NaN. The math might be off but you get the idea.

commented

200dc2e

(forgot "closes #3")