Hướng dẫn get timezone from country code javascript - lấy múi giờ từ mã quốc gia javascript
Tôi sẽ trả lời câu hỏi này cho Node.js bằng cách sử dụng TypeScript (vui lòng xóa các loại nếu bạn muốn sử dụng với kế hoạch JavaScript). Đối với điều đó, chúng tôi sẽ cần 2 gói NPM. Show
Tuyên bố miễn trừ trách nhiệm: Timezones rất phức tạp. Không phải tất cả các múi giờ đều cách nhau 1 giờ và một số cài đặt thời gian tiết kiệm ánh sáng ban ngày là kỳ lạ, ví dụ, chênh lệch 15 phút thay vì 60 phút tiêu chuẩn. Đây là một triển khai ngây thơ phù hợp với trường hợp sử dụng của tôi. Sử dụng một cách quyết định.Timezones are COMPLEX. Not all timezones are 1 hour apart and some daylight saving time settings are weird with, for example, 15 minutes difference instead of the standard 60 minutes. This is a naive implementation that suited my use case. Use with discretion. Code:
Bài kiểm tra đơn vị (với jest) 3.3.0 & nbsp; • & nbsp; public & nbsp; • & nbsp; xuất bản một năm trướcPublic • Published a year ago
Cách sử dụngNodejsCài đặt với NPM hoặc Sợi: npm install --save countries-and-timezones Trình duyệtThêm tập lệnh sau vào dự án của bạn (chỉ ~ 9kb): <script src="https://cdn.jsdelivr.net/gh/manuelmhtr/countries-and-timezones@latest/dist/index.min.js" type="text/javascript">script> <script src="https://cdn.jsdelivr.net/gh/manuelmhtr//dist/index.min.js" type="text/javascript">script> <script type="text/javascript"> var data = ct.getCountry('MX'); console.log(data); script> APIimport { getUtcOffsetForLocation } from "../timezone"; describe("timezone", () => { describe("getUtcOffsetForLocation", () => { it("should work for Lisbon", () => { expect(getUtcOffsetForLocation("Lisbon")).toEqual([0]); }); it("should work for Berlin", () => { expect(getUtcOffsetForLocation("Berlin")).toEqual([1]); }); it("should work for Germany", () => { expect(getUtcOffsetForLocation("Germany")).toEqual([1]); }); it("should work for the United States", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("United States")).toEqual( [-10, -9, -8, -7, -6, -5, -4]); }); it("should return null for a non-existing region", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("Blablabla")).toEqual( null); }); }); }); 3Trả về một quốc gia được tham chiếu bởi 4.Chấp nhận một tham số với 5.Thí dụ const ct = require('countries-and-timezones'); const country = ct.getCountry('DE'); console.log(country); /* Prints: { id: 'DE', name: 'Germany', timezones: [ 'Europe/Berlin', 'Europe/Zurich' ] } */ import { getUtcOffsetForLocation } from "../timezone"; describe("timezone", () => { describe("getUtcOffsetForLocation", () => { it("should work for Lisbon", () => { expect(getUtcOffsetForLocation("Lisbon")).toEqual([0]); }); it("should work for Berlin", () => { expect(getUtcOffsetForLocation("Berlin")).toEqual([1]); }); it("should work for Germany", () => { expect(getUtcOffsetForLocation("Germany")).toEqual([1]); }); it("should work for the United States", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("United States")).toEqual( [-10, -9, -8, -7, -6, -5, -4]); }); it("should return null for a non-existing region", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("Blablabla")).toEqual( null); }); }); }); 6Trả về một múi giờ được tham chiếu bởi 7 của nó.Thí dụ const ct = require('countries-and-timezones'); const timezone = ct.getTimezone('America/Los_Angeles'); console.log(timezone); /* Prints: { name: 'America/Los_Angeles', countries: [ 'US' ], utcOffset: -480, utcOffsetStr: '-08:00', dstOffset: -420, dstOffsetStr: '-07:00', aliasOf: null } */ import { getUtcOffsetForLocation } from "../timezone"; describe("timezone", () => { describe("getUtcOffsetForLocation", () => { it("should work for Lisbon", () => { expect(getUtcOffsetForLocation("Lisbon")).toEqual([0]); }); it("should work for Berlin", () => { expect(getUtcOffsetForLocation("Berlin")).toEqual([1]); }); it("should work for Germany", () => { expect(getUtcOffsetForLocation("Germany")).toEqual([1]); }); it("should work for the United States", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("United States")).toEqual( [-10, -9, -8, -7, -6, -5, -4]); }); it("should return null for a non-existing region", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("Blablabla")).toEqual( null); }); }); }); 6Trả về một múi giờ được tham chiếu bởi 7 của nó.Chấp nhận một tham số với 5.Thí dụ const ct = require('countries-and-timezones'); const countries = ct.getAllCountries(); console.log(countries); /* Prints: { AD: { id: 'AD', name: 'Andorra', timezones: [ 'Europe/Andorra' ] }, AE: { id: 'AE', name: 'United Arab Emirates', timezones: [ 'Asia/Dubai' ] }, AF: { id: 'AF', name: 'Afghanistan', timezones: [ 'Asia/Kabul' ] }, AG: { id: 'AG', name: 'Antigua and Barbuda', timezones: [ 'America/Puerto_Rico' ] }, ... } */ import { getUtcOffsetForLocation } from "../timezone"; describe("timezone", () => { describe("getUtcOffsetForLocation", () => { it("should work for Lisbon", () => { expect(getUtcOffsetForLocation("Lisbon")).toEqual([0]); }); it("should work for Berlin", () => { expect(getUtcOffsetForLocation("Berlin")).toEqual([1]); }); it("should work for Germany", () => { expect(getUtcOffsetForLocation("Germany")).toEqual([1]); }); it("should work for the United States", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("United States")).toEqual( [-10, -9, -8, -7, -6, -5, -4]); }); it("should return null for a non-existing region", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("Blablabla")).toEqual( null); }); }); }); 6Trả về một múi giờ được tham chiếu bởi 7 của nó.Chấp nhận một tham số với 5.Thí dụ const ct = require('countries-and-timezones'); const timezones = ct.getAllTimezones(); console.log(timezones); /* Prints: { "Africa/Abidjan": { "name": "Africa/Abidjan", "countries": [ "CI", "BF", "GH", "GM", "GN", "ML", "MR", "SH", "SL", "SN", "TG" ], "utcOffset": 0, "utcOffsetStr": "+00:00", "dstOffset": 0, "dstOffsetStr": "+00:00", "aliasOf": null }, "Africa/Algiers": { "name": "Africa/Algiers", "countries": [ "DZ" ], "utcOffset": 60, "utcOffsetStr": "+01:00", "dstOffset": 60, "dstOffsetStr": "+01:00", "aliasOf": null }, "Africa/Bissau": { "name": "Africa/Bissau", "countries": [ "GW" ], "utcOffset": 0, "utcOffsetStr": "+00:00", "dstOffset": 0, "dstOffsetStr": "+00:00", "aliasOf": null }, ... } */ import { getUtcOffsetForLocation } from "../timezone"; describe("timezone", () => { describe("getUtcOffsetForLocation", () => { it("should work for Lisbon", () => { expect(getUtcOffsetForLocation("Lisbon")).toEqual([0]); }); it("should work for Berlin", () => { expect(getUtcOffsetForLocation("Berlin")).toEqual([1]); }); it("should work for Germany", () => { expect(getUtcOffsetForLocation("Germany")).toEqual([1]); }); it("should work for the United States", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("United States")).toEqual( [-10, -9, -8, -7, -6, -5, -4]); }); it("should return null for a non-existing region", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("Blablabla")).toEqual( null); }); }); }); 6Trả về một múi giờ được tham chiếu bởi 7 của nó.Chấp nhận một tham số với 5.Thí dụ const ct = require('countries-and-timezones'); const timezones = ct.getTimezonesForCountry('MX'); console.log(timezones); /* Prints: [ { "name": "America/Bahia_Banderas", "countries": [ "MX" ], "utcOffset": -360, "utcOffsetStr": "-06:00", "dstOffset": -300, "dstOffsetStr": "-05:00", "aliasOf": null }, { "name": "America/Cancun", "countries": [ "MX" ], "utcOffset": -300, "utcOffsetStr": "-05:00", "dstOffset": -300, "dstOffsetStr": "-05:00", "aliasOf": null }, { "name": "America/Chihuahua", "countries": [ "MX" ], "utcOffset": -420, "utcOffsetStr": "-07:00", "dstOffset": -360, "dstOffsetStr": "-06:00", "aliasOf": null }, ... } */ import { getUtcOffsetForLocation } from "../timezone"; describe("timezone", () => { describe("getUtcOffsetForLocation", () => { it("should work for Lisbon", () => { expect(getUtcOffsetForLocation("Lisbon")).toEqual([0]); }); it("should work for Berlin", () => { expect(getUtcOffsetForLocation("Berlin")).toEqual([1]); }); it("should work for Germany", () => { expect(getUtcOffsetForLocation("Germany")).toEqual([1]); }); it("should work for the United States", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("United States")).toEqual( [-10, -9, -8, -7, -6, -5, -4]); }); it("should return null for a non-existing region", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("Blablabla")).toEqual( null); }); }); }); 6Trả về một múi giờ được tham chiếu bởi 7 của nó.the first element is more relevant due to its geographical location.Chấp nhận một tham số với 5.Thí dụ const ct = require('countries-and-timezones'); const timezone = ct.getCountriesForTimezone('Europe/Zurich'); console.log(timezone); /* Prints: [ { "id": "CH", "name": "Switzerland", "timezones": [ "Europe/Zurich" ] }, { "id": "DE", "name": "Germany", "timezones": [ "Europe/Berlin", "Europe/Zurich" ] }, { "id": "LI", "name": "Liechtenstein", "timezones": [ "Europe/Zurich" ] } ] */ import { getUtcOffsetForLocation } from "../timezone"; describe("timezone", () => { describe("getUtcOffsetForLocation", () => { it("should work for Lisbon", () => { expect(getUtcOffsetForLocation("Lisbon")).toEqual([0]); }); it("should work for Berlin", () => { expect(getUtcOffsetForLocation("Berlin")).toEqual([1]); }); it("should work for Germany", () => { expect(getUtcOffsetForLocation("Germany")).toEqual([1]); }); it("should work for the United States", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("United States")).toEqual( [-10, -9, -8, -7, -6, -5, -4]); }); it("should return null for a non-existing region", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("Blablabla")).toEqual( null); }); }); }); 6Trả về một múi giờ được tham chiếu bởi 7 của nó.Chấp nhận một tham số với 5.Thí dụ 0import { getUtcOffsetForLocation } from "../timezone"; describe("timezone", () => { describe("getUtcOffsetForLocation", () => { it("should work for Lisbon", () => { expect(getUtcOffsetForLocation("Lisbon")).toEqual([0]); }); it("should work for Berlin", () => { expect(getUtcOffsetForLocation("Berlin")).toEqual([1]); }); it("should work for Germany", () => { expect(getUtcOffsetForLocation("Germany")).toEqual([1]); }); it("should work for the United States", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("United States")).toEqual( [-10, -9, -8, -7, -6, -5, -4]); }); it("should return null for a non-existing region", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("Blablabla")).toEqual( null); }); }); }); 6Trả về một múi giờ được tham chiếu bởi 7 của nó.
npm install --save countries-and-timezones5Trả về một danh sách các quốc gia sử dụng múi giờ cho import { getUtcOffsetForLocation } from "../timezone"; describe("timezone", () => { describe("getUtcOffsetForLocation", () => { it("should work for Lisbon", () => { expect(getUtcOffsetForLocation("Lisbon")).toEqual([0]); }); it("should work for Berlin", () => { expect(getUtcOffsetForLocation("Berlin")).toEqual([1]); }); it("should work for Germany", () => { expect(getUtcOffsetForLocation("Germany")).toEqual([1]); }); it("should work for the United States", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("United States")).toEqual( [-10, -9, -8, -7, -6, -5, -4]); }); it("should return null for a non-existing region", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("Blablabla")).toEqual( null); }); }); }); 7. Khi một múi giờ có nhiều quốc gia, yếu tố đầu tiên có liên quan hơn do vị trí địa lý của nó.
npm install --save countries-and-timezones8
1Trả về một quốc gia có liên quan nhất (do vị trí địa lý của nó) sử dụng múi giờ được cho import { getUtcOffsetForLocation } from "../timezone"; describe("timezone", () => { describe("getUtcOffsetForLocation", () => { it("should work for Lisbon", () => { expect(getUtcOffsetForLocation("Lisbon")).toEqual([0]); }); it("should work for Berlin", () => { expect(getUtcOffsetForLocation("Berlin")).toEqual([1]); }); it("should work for Germany", () => { expect(getUtcOffsetForLocation("Germany")).toEqual([1]); }); it("should work for the United States", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("United States")).toEqual( [-10, -9, -8, -7, -6, -5, -4]); }); it("should return null for a non-existing region", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("Blablabla")).toEqual( null); }); }); }); 7.
5
2npm install --save countries-and-timezones5
Trả về một quốc gia có liên quan nhất (do vị trí địa lý của nó) sử dụng múi giờ được cho import { getUtcOffsetForLocation } from "../timezone"; describe("timezone", () => { describe("getUtcOffsetForLocation", () => { it("should work for Lisbon", () => { expect(getUtcOffsetForLocation("Lisbon")).toEqual([0]); }); it("should work for Berlin", () => { expect(getUtcOffsetForLocation("Berlin")).toEqual([1]); }); it("should work for Germany", () => { expect(getUtcOffsetForLocation("Germany")).toEqual([1]); }); it("should work for the United States", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("United States")).toEqual( [-10, -9, -8, -7, -6, -5, -4]); }); it("should return null for a non-existing region", () => { // when the region has multiple timezones, expect(getUtcOffsetForLocation("Blablabla")).toEqual( null); }); }); }); 7.
5Các tùy chọn có sẵn cho các chức năng là:Tham số
BooleanCho biết nếu kết quả có nên bao gồm thời gian không dùng nữa hay không. Theo mặc định, không có thời gian không dùng nữa được bao gồm. Làm thế nào để tôi tìm thấy múi giờ của tôi trong JavaScript?Để có được múi giờ của trình duyệt hiện tại, bạn có thể sử dụng phương thức GetTimeZoneOffset () từ đối tượng Ngày JavaScript. GetTimeZoneOffset () trả về chênh lệch thời gian, tính theo phút, giữa thời gian UTC và thời gian địa phương.use the getTimezoneOffset() method from the JavaScript Date object. The getTimezoneOffset() returns the time difference, in minutes, between UTC time and local time.
GetTimezoneOffset trong JavaScript là gì?GetTimeZoneOffset () Phương thức GetTimeZoneOffset () trả về sự khác biệt, tính theo phút, giữa một ngày được đánh giá trong múi giờ UTC và cùng ngày được đánh giá trong múi giờ địa phương.returns the difference, in minutes, between a date as evaluated in the UTC time zone, and the same date as evaluated in the local time zone.
Làm thế nào để tôi có được múi giờ thời gian?Ví dụ: nếu bạn phân tích cú pháp chuỗi '2020/01/02' và sau đó gọi phương thức season#tz (), bạn đang kể khoảnh khắc phân tích chuỗi trong thời gian địa phương, sau đó chuyển đổi ngày thành một thời gian IANA đã cho.// '20200101 21:00 PST' MOMENT ('2020/01/02', 'yyyy/mm/dd').Tz ('Mỹ/los_angele').định dạng ('yyyymmdd hh: mm z');if you parse the string '2020/01/02' and then call the moment#tz() method, you're telling moment to parse the string in the locale time, and then convert the date to a given IANA timezone. // '20200101 21:00 PST' moment('2020/01/02', 'YYYY/MM/DD'). tz('America/Los_Angeles'). format('YYYYMMDD HH:mm z');
Làm thế nào để tôi tìm thấy múi giờ theo ngày?Phương thức GetTimezone () trong lớp DateFormat được sử dụng để trả về vùng thời gian hiện tại của lịch của ngày này .. Syntax:. Tham số: Phương thức không lấy bất kỳ tham số nào .. Giá trị trả về: Phương thức trả về múi giờ được liên kết với lịch của DateFormat .. |