Artvader
Artvader

Reputation: 958

Getting the Time Zone (just the timezone) using angular

I'm not sure if this is possible, but I want to get the PC's Time Zone using Angular. I just need the timezone because I need to format it in ('+H:MM').

I understand Angular only formats it one way ('+HHMM'), so I want to store this Time Zone and put in a string that I can then manipulate later.

Also I don't want to use plugins and/or libraries, if possible.

Upvotes: 2

Views: 12475

Answers (4)

Asif Karim Bherani
Asif Karim Bherani

Reputation: 1083

Might be late answer but just adding in case if it helps someone else. moment-timezone package allows you can guess at the local time zone, Install moment-timezone using:

npm i moment-timezone -- save

import moment from 'moment-timezone'

const timezone = moment.tz(moment.tz.guess()).format('z');
console.log(timezone); // eg: PST

This is ok, but be aware that:

  1. It's just a guess. It might guess wrong.
  2. If it does guess wrong, there's still a possibility that the abbreviation could be correct, as many similar time zones will use the same abbreviations, such as how Europe/Paris and Europe/Berlin both use CET and CEST.
  3. However, there's no guarantees. If it guesses wrong, you might present the wrong abbreviation.

Upvotes: 1

Jeremy
Jeremy

Reputation: 1002

How about this?

let timeZone;
if (typeof Intl === 'object' && typeof Intl.DateTimeFormat === 'function') {
  timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
  console.log({ timeZone });
}

Upvotes: 4

thegio
thegio

Reputation: 1243

I suggest you to use Momentjs:

http://momentjs.com

Momentjs Formats

var from = new Date();
var date = moment(from).format('Z'); //ZZ

alert(date);

Fiddle

Or you can perform a regular expression on the Date:

var from = new Date();
var res = from.toString().match(/[\+,\-](\d{4})\s/g);
alert(res);

Fiddle

But I'm not sure it is very robust.

I hope it helps.

Upvotes: 3

Vahid Alvandi
Vahid Alvandi

Reputation: 612

if you need get internet time use this site

http://www.timeapi.org/utc/now.json

Upvotes: 0

Related Questions