Reputation: 958
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
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:
Upvotes: 1
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
Reputation: 1243
I suggest you to use Momentjs:
var from = new Date();
var date = moment(from).format('Z'); //ZZ
alert(date);
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);
But I'm not sure it is very robust.
I hope it helps.
Upvotes: 3
Reputation: 612
if you need get internet time use this site
http://www.timeapi.org/utc/now.json
Upvotes: 0