npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

shift-timezone-offset

v0.0.3

Published

Shift timezoned dates to local time or any other timezone.

Downloads

5

Readme

shift-timezone-offset

Converts timezoned dates to local time or any other timezone.

Sometimes it's easier to work in local time, because all JavaScript Date objects are given in local time:

new Date('2017-01-01T00:00:00Z').getHours()

// => 11 

It's because my timezone is +11:00, fine, but I would like a 0!

This library will shift a date to match time in the local timezone, or the one you need, as if the timezone information is ignored.

example

Working in local time:

// process.env.TZ = 'Pacific/Noumea' // local timezone

var timezoneShift = require('../')
var dateInfo = '2017-01-01T00:00:00+03:00'
var initialDate = new Date(dateInfo)

console.log(initialDate.getHours())
// => 8

console.log(initialDate.getTime() / 1000)
// => 1483218000

var converter = timezoneShift('+03:00') // timezoneShift(dateInfo) works as well
var localDate = converter.toLocal(initialDate)

console.log(localDate.getHours())
// => 0

console.log(localDate.getTime() / 1000)
// => 1483189200

var backToInitialDate = converter.fromLocal(localDate)

console.log(backToInitialDate.getHours())
// => 8

console.log(backToInitialDate.getTime() / 1000)
// => 1483218000

When only one operation is needed, there is a shortcut notation:

var timezoneShift = require('../')
var dateInfo = '2017-01-01T12:00:00+03:00'

console.log(new Date(dateInfo))
// => 2017-01-01T09:00:00.000Z

// 5 hours timezone shift
console.log(timezoneShift.convert(dateInfo, { from: '+03:00', to: '+08:00' }))
// => 2017-01-01T04:00:00.000Z

// to local (all 3 are similar)
// "to" defaults to local timezone:
console.log(timezoneShift.convert(dateInfo, { from: '+03:00' }))
// => 2017-01-01T01:00:00.000Z

// to be sure:
console.log(timezoneShift.convert(dateInfo, { from: '+03:00', to: '+11:00' }))
// => 2017-01-01T01:00:00.000Z

// even simpler ("from" timezone is infered from date string):
console.log(timezoneShift.convert(dateInfo)) 
// => 2017-01-01T01:00:00.000Z

api

var timezoneShift = require('timezone-offset')

var converter = timezoneShift(validTimezone)

  • validTimezone - valid ISO 8601 date string or timezone string: '+05:00', '-1100' or '2017-01-01T12:00:00+05:00'. An offset is valid as well: -240 in place of '+04:00.

converter.toLocal(date)

Shift a date considered in the converter timezone to the local timezone. It means the absolute UTC value is affected. Returns a new Date object.

  • date - any valid value for the Date constructor.

converter.fromLocal(date)

Shift a local date to the timezone. It means the UTC value is affected. Returns a new Date object.

  • date - any valid value for the Date constructor.

converter.toTimezone(date, zone)

Shift a local date to converter timezone. Returns a new Date object.

  • date - any valid value for the Date constructor.
  • zone - valid ISO 8601 date string or timezone string.

converter.offset

Timezone offset from UTC in minutes (similar to Date.prototype.getTimezoneOffset).

converter.timeShift

The full shift in minutes required to convert the dates to the local timezone.

timezoneShift.convert(date, zones)

  • date - any valid value for the Date object constructor.
  • zone - object containing initial and final timezones:
    • zones.from - valid timezone info. Could be infered if date is an ISO date string.
    • zones.to - default: local timezone. Valid timezone info.

license

MIT

install

npm install shift-timezone-offset