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
  • moment-timezone
  • city-timezones

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:

import * as cityTimeZones from "city-timezones";
import * as moment from "moment-timezone";

/**
 * Returns the UTC offset for the given timezone
 * @param timezone Example: America/New_York
 */
export function getNormalizedUtcOffset(timezone: string): number | null {
  const momentTimezone = moment.tz(timezone);
  if (!momentTimezone) {
    return null;
  }
  let offset = momentTimezone.utcOffset();
  if (momentTimezone.isDST()) {
    // utcOffset will return the offset normalized by DST. If the location
    // is in daylight saving time now, it will be adjusted for that. This is
    // a NAIVE attempt to normalize that by going back 1 hour
    offset -= 60;
  }
  return offset/60;
}

/**
 * Returns the offset range for the given city or region
 * @param location
 */
export function getUtcOffsetForLocation(location: string): number[] | null {
  const timezones = cityTimeZones.findFromCityStateProvince(location);
  if (timezones && timezones.length) {
    // timezones will contain an array of all timezones for all cities inside
    // the given location. For example, if location is a country, this will contain
    // all timezones of all cities inside the country.
    // YOU SHOULD CACHE THE RESULT OF THIS FUNCTION.
    const offsetSet = new Set();
    for (let timezone of timezones) {
      const offset = getNormalizedUtcOffset(timezone.timezone);
      if (offset !== null) {
        offsetSet.add(offset);
      }
    }

    return [...offsetSet].sort((a, b) => a - b);
  }
  return null;
}

Bài kiểm tra đơn vị (với jest)

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);
    });
  });
});

3.3.0 & nbsp; • & nbsp; public & nbsp; • & nbsp; xuất bản một năm trướcPublic • Published a year ago

  • Readme
  • Khám phá BetaBETA
  • 0 phụ thuộc
  • 88 người phụ thuộc
  • 24 phiên bản

Các quốc gia và thời gian

Hướng dẫn get timezone from country code javascript - lấy múi giờ từ mã quốc gia javascript

Thư viện tối giản để làm việc với dữ liệu của các quốc gia và múi giờ. Cập nhật với cơ sở dữ liệu IANA Timezones.

Cách sử dụng

Nodejs

Cài đặt với NPM hoặc Sợi:

npm install --save countries-and-timezones

Trình duyệt

Thê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>

API

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); }); }); }); 3

Trả về một quốc gia được tham chiếu bởi

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);
    });
  });
});
4.

Chấp nhận một tham số với

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);
    });
  });
});
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); }); }); }); 6

Trả về một múi giờ được tham chiếu bởi

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 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); }); }); }); 6

Trả về một múi giờ được tham chiếu bởi

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 của nó.

Chấp nhận một tham số với

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);
    });
  });
});
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); }); }); }); 6

Trả về một múi giờ được tham chiếu bởi

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 của nó.

Chấp nhận một tham số với

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);
    });
  });
});
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); }); }); }); 6

Trả về một múi giờ được tham chiếu bởi

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 của nó.

Chấp nhận một tham số với

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);
    });
  });
});
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); }); }); }); 6

Trả về một múi giờ được tham chiếu bởi

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 của nó.the first element is more relevant due to its geographical location.

Chấp nhận một tham số với

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);
    });
  });
});
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); }); }); }); 6

Trả về một múi giờ được tham chiếu bởi

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 của nó.

Chấp nhận một tham số với

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);
    });
  });
});
5.

Thí dụ

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);
    });
  });
});
0

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); }); }); }); 6

Trả về một múi giờ được tham chiếu bởi

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 của nó.

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);
    });
  });
});
8
Trả về một đối tượng với dữ liệu của tất cả các quốc gia.
npm install --save countries-and-timezones
0
Trả về một đối tượng với dữ liệu của tất cả các múi giờ.
npm install --save countries-and-timezones
2
Trả về một mảng với tất cả thời gian của một quốc gia được đưa ra
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);
    });
  });
});
4.

npm install --save countries-and-timezones5

Trả 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-timezones
8

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);
    });
  });
});
8
Trả về một đối tượng với dữ liệu của tất cả các quốc gia.
npm install --save countries-and-timezones
0
Trả về một đối tượng với dữ liệu của tất cả các múi giờ.
npm install --save countries-and-timezones
2
Trả về một mảng với tất cả thời gian của một quốc gia được đưa ra
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);
    });
  });
});
4.
npm install --save countries-and-timezones
5
npm install --save countries-and-timezones
2
Trả về một mảng với tất cả thời gian của một quốc gia được đưa ra
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);
    });
  });
});
4.
npm install --save countries-and-timezones
5
Trả 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-timezones
8

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);
    });
  });
});
1

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.

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);
    });
  });
});
5

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);
    });
  });
});
8
Trả về một đối tượng với dữ liệu của tất cả các quốc gia.
npm install --save countries-and-timezones
0
npm install --save countries-and-timezones
5
npm install --save countries-and-timezones
2
Trả về một mảng với tất cả thời gian của một quốc gia được đưa ra
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);
    });
  });
});
4.
npm install --save countries-and-timezones
5
Trả 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-timezones
8
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.
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);
    });
  });
});
5
Các tùy chọn có sẵn cho các chức năng là:minutes between the timezone and UTC.
Tham số
npm install --save countries-and-timezones
2
Trả về một mảng với tất cả thời gian của một quốc gia được đưa ra
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);
    });
  });
});
4.
npm install --save countries-and-timezones
5
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);
    });
  });
});
5
Các tùy chọn có sẵn cho các chức năng là:minutes between the timezone and UTC during daylight saving time (DST). When
const ct = require('countries-and-timezones');

const country = ct.getCountry('DE');
console.log(country);

/*
Prints:

{
  id: 'DE',
  name: 'Germany',
  timezones: [ 'Europe/Berlin', 'Europe/Zurich' ]
}

*/
1 and
const ct = require('countries-and-timezones');

const country = ct.getCountry('DE');
console.log(country);

/*
Prints:

{
  id: 'DE',
  name: 'Germany',
  timezones: [ 'Europe/Berlin', 'Europe/Zurich' ]
}

*/
4 are the same, means that the timezone does not observe a daylight saving time.
Tham số
npm install --save countries-and-timezones
2
Trả về một mảng với tất cả thời gian của một quốc gia được đưa ra
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);
    });
  });
});
4.
npm install --save countries-and-timezones
5
npm install --save countries-and-timezones
2
Trả về một mảng với tất cả thời gian của một quốc gia được đưa ra
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);
    });
  });
});
4.
Trả về một đối tượng với dữ liệu của tất cả các múi giờ.
npm install --save countries-and-timezones
2
Trả về một mảng với tất cả thời gian của một quốc gia được đưa ra
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);
    });
  });
});
4.

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);
    });
  });
});
2

npm install --save countries-and-timezones5

  • Trả 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-timezones
    8

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.

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);
    });
  });
});
5

Các tùy chọn có sẵn cho các chức năng là:

Tham số

  • Loại hình in the world. By full strings or autocompletion.
  • Sự mô tả
  • 
    <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>
    2

Boolean

Cho 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 ..