GithubHelp home page GithubHelp logo

prantlf / date-fns-timezone Goto Github PK

View Code? Open in Web Editor NEW
137.0 3.0 19.0 634 KB

Parsing and formatting date strings using IANA time zones for date-fns.

License: MIT License

JavaScript 96.06% TypeScript 3.94%
date-fns timezone time-zone date time

date-fns-timezone's Introduction

date-fns-timezone

NPM version Build Status Coverage Status Codacy Badge Dependency Status devDependency Status JavaScript Style Guide

Provides parsing and formatting date strings and time zone conversions supporting IANA time zones, following the design of functions in date-fns. List of canonical time zone names is provided by timezone-support.

Table of Contents

Synopsis

const { listTimeZones } = require('timezone-support')
const { parseFromTimeZone, formatToTimeZone } = require('date-fns-timezone')

// List canonical time zone names: [ 'Africa/Abidjan', ... ]
const timeZones = listTimeZones()

// Set the date to "2018-09-01T16:01:36.386Z"
const date = parseFromTimeZone('2018-09-01 18:01:36.386', { timeZone: 'Europe/Berlin' })

// Set the output to "1.9.2018 18:01:36.386 GMT+02:00 (CEST)"
const date = new Date('2018-09-01Z16:01:36.386Z')
const format = 'D.M.YYYY HH:mm:ss.SSS [GMT]Z (z)'
const output = formatToTimeZone(date, format, { timeZone: 'Europe/Berlin' })

Installation and Getting Started

This module can be installed in your project using NPM or Yarn. Make sure, that you use Node.js version 6 or newer.

$ npm i date-fns-timezone --save
$ yarn add date-fns-timezone

Functions are exposed as named exports from the package modules, for example:

const { parseFromTimeZone, formatToTimeZone } = require('date-fns-timezone')

You can read more about the module loading in other environments, like with ES6 or in web browsers. Usage scenarios demonstrate applications of this library in typical real-world scenarios. The API reference lists all functions with a description of their functionality.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

  • 2018-10-10 v0.1.4 Fix compatibility with IE. Thanks, Andrii!
  • 2018-10-06 v0.1.2 Add TypeScript export declarations.
  • 2018-09-19 v0.1.0 Add parseString without a time zone to cover a gap in date-fns
  • 2018-09-17 v0.0.1 Initial release

License

Copyright (c) 2018 Ferdinand Prantl

Licensed under the MIT license.

date-fns-timezone's People

Contributors

bertho-zero avatar lgaticaq avatar prantlf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

date-fns-timezone's Issues

Asia/Kolkata vs Asia/Calcutta

I believe there are inconsistencies with the naming for the timezone affecting Mumbai. Isn't the convention Asia/Kolkata. Causing my app to crash.

Z character not escaped when put in square brackets

Let say I have some date in a variable called dateObject (1 July 2019 05:00:00 UTC), then doing:

formatToTimeZone(dateObject, 'DDHHmm[Z] MMM YYYY', { timeZone: 'UTC' })

should give:

010500Z Jul 2019

but instead gives:

010500+00:00 Jul 2019

I thought doing [Z] escapes the Z like the M in [GMT]????

Support for limited 2012-2022 timezone data

Nice library! date-fns really needs this timezone support built into the library.

Is there any support for only including the 2012-2022 timezone-support data?

Currently it looks like imports are coming from the timezone-support index which includes all of the data by default.

US/Central Time 1 Hour Off

The following line of code gives the value 18 which is WRONG. 00:00 UTC is 19:00 US/Central NOT 18:00 US/Central.

formatToTimeZone(new Date(Date.UTC(0, 0, 0, 0, 0, 0)), 'HH', {
  timeZone: 'US/Central'
});

IE error occured

Hi.
I've got an error in IE "Expected identifier". As I understood it caused bacause timezone-support is into ES5. Because in debug mode i see
function c({year:n,month:e,day:l,hours:t,minutes:o,seconds:i=0,milliseconds:u=0}){return Date.UTC(n,e-1,l,t,o,i,u)}.

Could you check please ?

Convert to timezone returning a diffrent value from moment-timzone.

Hi Guys,

I'm currently replacing moment-js with date-fns, but when it comes to timezone, I'm unable to get the same value.

Do you guys know what am I doing wrong?

const moment = require('moment-timezone');
const dateFNS = require('date-fns');
const dateFNSTimezone =  require('date-fns-timezone');

const { format } = dateFNS;
const { convertToTimeZone } = dateFNSTimezone;
  
const TZ = 'America/Los_Angeles';

const a = moment().tz(TZ).format();

const b = format(convertToTimeZone(new Date(), { timeZone: TZ }), 'YYYY-MM-DDTHH:mm:ssZ');

console.log(a); // 2019-02-11T09:28:29-08:00
console.log(b); // 2019-02-11T09:28:29-02:00

if you notice the values are equal, execept the last part, where moment gives -08:00 but date-fns-timezone returns -02:00

formatToTimeZone depends unexpectedly on system timezone

Using formatToTimeZone to convert an absolute time value to a date/time localised to a timezone gives different results depending on system timezone.

When converting an absolute number of milliseconds since epoch into an arbitrary timezone, I would not expect the local timezone to have any effect.

For example, compare system timezone as Europe/London:

$ TZ=Europe/London node
> const {formatToTimeZone} = require('date-fns-timezone');
undefined
> formatToTimeZone( 1585422000000, 'YYYY-MM-DDTHH:mm:ssZ', {timeZone: 'Pacific/Auckland'})
'2020-03-29T09:00:00+13:00'

with system timezone America/New_York:

$ TZ=America/New_York node
> const {formatToTimeZone} = require('date-fns-timezone');
undefined
> formatToTimeZone( 1585422000000, 'YYYY-MM-DDTHH:mm:ssZ', {timeZone: 'Pacific/Auckland'})
'2020-03-29T08:00:00+13:00'

The hour part is different : 09 vs 08

According to [1] the correct value is 08.

[1] https://www.epochconverter.com/timezones?q=1585422000000&tz=Pacific%2FAuckland

Plans for date-fns v2?

Great job in making it possible to work with timezones using date-fns v1! How well does this work with date-fns v2 alpha currently? Will this library live on after v2 release or deprecated once v2 is available and date-fns/date-fns#707 is merged? Or will you port your changes to a new PR for v2?

How to set a new Date() value with dynamic timezone

How can I create a new Date() value set to a specific timezone -- and get the timezone offset programatically? Right now I'm hard coding the timezone but these change throughout the year with daylight savings etc.

var dateInTimezone = new Date("2020-03-03 10:00:00 UTC+8:00");

Is there a way to set new Date( date + getTimezoneVal )?

Example scenario:
Submitting a date (ie. 2020-03-03 @ 10am) via a form -- but set the timestamp to 2020-03-03 10am HKT while you're in NYC.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.