diff --git a/README.md b/README.md index 82ddd8c..98ef505 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ assert.equal(ts, 1457856000000) // one hour later in UTC time... ts = zt(ts, 'America/Chicago') // 13 Mar, 2016 03:00 CDT assert.equal(ts, 1457838000000) // ...is 2 hours later in local time, - // because Daylight Savings Time + // because Daylight Savings Time // went into effect // Date objects can be used instead of timestamps... @@ -37,6 +37,7 @@ let d = new Date(Date.UTC(2016, 2, 13, 7, 2, 3)) // ...and you'll get back a Date object in return... d = zt(d, 'America/Chicago') +assert(d instanceof Date) // ...which is convenient for formatted display in local time: @@ -74,6 +75,14 @@ ts = zt(ts, 'America/Chicago') // UTC to local ts = zt(ts, 'America/Chicago', true) // local to UTC assert.equal(ts, Date.UTC(2016, 2, 13, 8)) +// It works for Date objects too: + +d = new Date(Date.UTC(2016, 2, 13, 8)) +d = zt(d, 'America/Chicago') // UTC to local +d = zt(d, 'America/Chicago', true) // local to UTC +assert(d instanceof Date) +assert.equal(d.getTime(), Date.UTC(2016, 2, 13, 8)) + // Offsets can also be retrieved for local to UTC conversions... ts = Date.UTC(2016, 2, 13, 8) diff --git a/src/core.js b/src/core.js index 0b879fa..a14dbc4 100644 --- a/src/core.js +++ b/src/core.js @@ -22,6 +22,6 @@ export function canonicalize (name) { } export default function tz (timestamp, name, local, opts) { - if (timestamp instanceof Date) return new Date(tz(timestamp.getTime(), name)) + if (timestamp instanceof Date) return new Date(tz(timestamp.getTime(), name, local, opts)) return timestamp + offset(timestamp, name, local, opts) * 6e4 }