All Projects β†’ iansinnott β†’ Jstz

iansinnott / Jstz

Licence: other
🌐Timezone detection for JavaScript

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Jstz

Swiftdate
πŸ” Toolkit to parse, validate, manipulate, compare and display dates, time & timezones in Swift.
Stars: ✭ 6,661 (+4225.32%)
Mutual labels:  timezone
Time
A simplified approach to working with dates, times, and time zones.
Stars: ✭ 83 (-46.1%)
Mutual labels:  timezone
Ticktock
A timezone data management library for the JVM and Android targeting java.time APIs in Java 8+
Stars: ✭ 122 (-20.78%)
Mutual labels:  timezone
Paper Timezone
Polymer based timezone selection component
Stars: ✭ 19 (-87.66%)
Mutual labels:  timezone
Google Time Zone
Get time zones for coordinates
Stars: ✭ 54 (-64.94%)
Mutual labels:  timezone
Js Joda
πŸ•‘ Immutable date and time library for javascript
Stars: ✭ 1,298 (+742.86%)
Mutual labels:  timezone
Valheim Docker
Valheim Docker powered by Odin. The Valheim dedicated gameserver manager which is designed with resiliency in mind by providing automatic updates, world backup support, and a user friendly cli interface.
Stars: ✭ 525 (+240.91%)
Mutual labels:  timezone
Delorean
Delorean: Time Travel Made Easy
Stars: ✭ 1,793 (+1064.29%)
Mutual labels:  timezone
Posix tz db
Generates POSIX timezones strings
Stars: ✭ 57 (-62.99%)
Mutual labels:  timezone
Date Fns Timezone
Parsing and formatting date strings using IANA time zones for date-fns.
Stars: ✭ 118 (-23.38%)
Mutual labels:  timezone
Time Zone Proposal
A proposal for accurately computing local time in JavaScript
Stars: ✭ 25 (-83.77%)
Mutual labels:  timezone
Tz offset
Simple abstraction of a timezone offset
Stars: ✭ 35 (-77.27%)
Mutual labels:  timezone
Luatz
Time, Date and Timezone library for lua
Stars: ✭ 92 (-40.26%)
Mutual labels:  timezone
Windows Iana
A small tool to convert Windows time zones to IANA
Stars: ✭ 17 (-88.96%)
Mutual labels:  timezone
Date Time
Date and time library for PHP
Stars: ✭ 128 (-16.88%)
Mutual labels:  timezone
Geolocator
A utility for getting geo-location information via HTML5 and IP look-ups, geocoding, address look-ups, distance and durations, timezone information and more...
Stars: ✭ 598 (+288.31%)
Mutual labels:  timezone
Timezone Support
Lightweight time zone support for your applications or other date libraries.
Stars: ✭ 90 (-41.56%)
Mutual labels:  timezone
Quantum Core
⌚ Cron-like job scheduler for Elixir
Stars: ✭ 1,905 (+1137.01%)
Mutual labels:  timezone
Tzupdate
Set the system timezone based on IP geolocation
Stars: ✭ 130 (-15.58%)
Mutual labels:  timezone
Timezonepicker
A TimeZonePicker UIViewController similar to the iOS Settings app. Search and select from a range of cities and countries to find your most suitable time zone.
Stars: ✭ 109 (-29.22%)
Mutual labels:  timezone

JSTZ

Circle CI JSTZ on NPM

Timezone detection for JavaScript

What

This library allows you to detect a user's timezone from within their browser. It is often useful to use JSTZ in combination with a timezone parsing library such as Moment Timezone.

This library is an unofficial fork of pellepim/jstimezonedetect. The original library works well and can be used via CDN, but it was not configured to work with NPM. This meant the library was less accessible because it could not be retrieved with a simple npm command or included as a dependency in package.json. Thus this fork was born.

Sidenote: If you're wondering why this isn't an actual GitHub fork it's because the original project uses Mercurial and is hosted on BitBucket.

Why

Dealing with timezones can be a pain. Libraries like Moment Timezone help a lot with the parsing side of things, but if you want to detect the users timezone you would normally have to do it manually. That's where this library comes in.

Usage

$ npm install --save jstz

In your JS file:

import jstz from 'jstz';
const timezone = jstz.determine();
timezone.name(); // => 'America/Los_Angeles' (or whatever your user's timezone is)

Or if you prefer ES5:

var jstz = require('jstz');
var timezone = jstz.determine();
timezone.name(); // => 'America/Los_Angeles' (or whatever your user's timezone is)

Note: If you're not using a module system such as Webpack or Browserify then I recommend you use the original library delivered via CDNJS:

<!doctype html>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.4/jstz.min.js'></script>
<script>
  var jstz = require('jstz');
  var timezone = jstz.determine();
  console.log('Your timezone is: ' + timezone.name());
</script>

Docs

To learn more about the library head on over to the original library's repo: https://bitbucket.org/pellepim/jstimezonedetect

Use With Rails

jstz is an excellent library to use by Rails to determine the time zone of the browser (e.g. the gem browser-timezone-rails), but some extra tweaking is necessary to make them play nicely together.

A common use case is to provide a time zone select (f.time_zone_select) where it defaults to the user's current time zone. That Rails helper uses ActiveSupport::TimeZone, which provides a more human-readable subset of the time zones (e.g. Eastern Time (US & Canada) instead of America/New_York). jstz doesn't know about this subset, so we need to use the TZInfo associated with those ActiveSupport::TimeZones to have a correct translation.

This method could go on your base application controller, assuming you're setting a browser cookie browser_time_zone:

# Returns the client's time zone based on a cookie set by the browser, defaults to application time zone
def browser_time_zone
  browser_tz = ActiveSupport::TimeZone.find_tzinfo(cookies[:browser_time_zone])
  ActiveSupport::TimeZone.all.find { |zone| zone.tzinfo == browser_tz } || Time.zone
rescue TZInfo::UnknownTimezone, TZInfo::InvalidTimezoneIdentifier
  Time.zone
end

Then in the view you could do something like:

<%= f.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones,
                       default: browser_time_zone.name %>

Further complicating matters, jstz uses the forward-thinking window.Intl, which has an awareness of time zones other than what is in Rails, so time zones such as America/Montreal from Intl will not be found in Rails. If you're using jstz with Rails, you will want to tempoarily "silence" the use of Intl when reading from jstz. Here's a code snippet:

export function findTimeZone() {
  const oldIntl = window.Intl
  try {
    window.Intl = undefined
    const tz = jstz.determine().name()
    window.Intl = oldIntl
    return tz
  } catch (e) {
    // sometimes (on android) you can't override intl
    return jstz.determine().name()
  }
}

Credits (from the original README.md)

Thanks to

Other contributors: Gilmore Davidson

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].