Hướng dẫn get timezone name javascript - lấy tên múi giờ javascript

206

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi biết làm thế nào để có được phần bù múi giờ, nhưng điều tôi cần là khả năng phát hiện một cái gì đó như "America/New York". Điều đó thậm chí có thể từ JavaScript hay đó là thứ mà tôi sẽ phải làm việc dựa trên phần bù?

hỏi ngày 19 tháng 3 năm 2012 lúc 15:27Mar 19, 2012 at 15:27

Hướng dẫn get timezone name javascript - lấy tên múi giờ javascript

Mike Thomsenmike ThomsenMike Thomsen

36K10 Huy hiệu vàng56 Huy hiệu bạc82 Huy hiệu Đồng10 gold badges56 silver badges82 bronze badges

3

API quốc tế hóa hỗ trợ nhận được múi giờ của người dùng và được hỗ trợ trong tất cả các trình duyệt hiện tại.

console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)

Hãy nhớ rằng trên một số phiên bản trình duyệt cũ hơn hỗ trợ API quốc tế hóa, thuộc tính

function getTimezoneName() {
  const today = new Date();
  const short = today.toLocaleDateString(undefined);
  const full = today.toLocaleDateString(undefined, { timeZoneName: 'long' });

  // Trying to remove date from the string in a locale-agnostic way
  const shortIndex = full.indexOf(short);
  if (shortIndex >= 0) {
    const trimmed = full.substring(0, shortIndex) + full.substring(shortIndex + short.length);
    
    // by this time `trimmed` should be the timezone's name with some punctuation -
    // trim it from both sides
    return trimmed.replace(/^[\s,.\-:;]+|[\s,.\-:;]+$/g, '');

  } else {
    // in some magic case when short representation of date is not present in the long one, just return the long one as a fallback, since it should contain the timezone's name
    return full;
  }
}

console.log(getTimezoneName());
0 được đặt thành
function getTimezoneName() {
  const today = new Date();
  const short = today.toLocaleDateString(undefined);
  const full = today.toLocaleDateString(undefined, { timeZoneName: 'long' });

  // Trying to remove date from the string in a locale-agnostic way
  const shortIndex = full.indexOf(short);
  if (shortIndex >= 0) {
    const trimmed = full.substring(0, shortIndex) + full.substring(shortIndex + short.length);
    
    // by this time `trimmed` should be the timezone's name with some punctuation -
    // trim it from both sides
    return trimmed.replace(/^[\s,.\-:;]+|[\s,.\-:;]+$/g, '');

  } else {
    // in some magic case when short representation of date is not present in the long one, just return the long one as a fallback, since it should contain the timezone's name
    return full;
  }
}

console.log(getTimezoneName());
1 thay vì chuỗi múi giờ của người dùng. Như tốt nhất tôi có thể nói, tại thời điểm viết (tháng 7 năm 2017), tất cả các trình duyệt hiện tại ngoại trừ IE11 sẽ trả về múi giờ của người dùng dưới dạng chuỗi.

Đã trả lời ngày 5 tháng 7 năm 2017 lúc 20:55Jul 5, 2017 at 20:55

Daniel Comptondaniel ComptonDaniel Compton

13.1k4 Huy hiệu vàng37 Huy hiệu bạc58 Huy hiệu đồng4 gold badges37 silver badges58 bronze badges

2

Hầu hết các câu trả lời được nâng cấp có lẽ là cách tốt nhất để có được múi giờ, tuy nhiên,

function getTimezoneName() {
  const today = new Date();
  const short = today.toLocaleDateString(undefined);
  const full = today.toLocaleDateString(undefined, { timeZoneName: 'long' });

  // Trying to remove date from the string in a locale-agnostic way
  const shortIndex = full.indexOf(short);
  if (shortIndex >= 0) {
    const trimmed = full.substring(0, shortIndex) + full.substring(shortIndex + short.length);
    
    // by this time `trimmed` should be the timezone's name with some punctuation -
    // trim it from both sides
    return trimmed.replace(/^[\s,.\-:;]+|[\s,.\-:;]+$/g, '');

  } else {
    // in some magic case when short representation of date is not present in the long one, just return the long one as a fallback, since it should contain the timezone's name
    return full;
  }
}

console.log(getTimezoneName());
2 trả về tên IANA MIMEZONE theo định nghĩa, bằng tiếng Anh.

Nếu bạn muốn tên của timezone bằng ngôn ngữ của người dùng hiện tại, bạn có thể phân tích nó từ biểu diễn chuỗi của ____ 13 như vậy:

function getTimezoneName() {
  const today = new Date();
  const short = today.toLocaleDateString(undefined);
  const full = today.toLocaleDateString(undefined, { timeZoneName: 'long' });

  // Trying to remove date from the string in a locale-agnostic way
  const shortIndex = full.indexOf(short);
  if (shortIndex >= 0) {
    const trimmed = full.substring(0, shortIndex) + full.substring(shortIndex + short.length);
    
    // by this time `trimmed` should be the timezone's name with some punctuation -
    // trim it from both sides
    return trimmed.replace(/^[\s,.\-:;]+|[\s,.\-:;]+$/g, '');

  } else {
    // in some magic case when short representation of date is not present in the long one, just return the long one as a fallback, since it should contain the timezone's name
    return full;
  }
}

console.log(getTimezoneName());

Được thử nghiệm trong Chrome và Firefox.

Tất nhiên, điều này sẽ không hoạt động như dự định trong một số môi trường. Ví dụ: Node.js trả về một bù gmt (ví dụ:

function getTimezoneName() {
  const today = new Date();
  const short = today.toLocaleDateString(undefined);
  const full = today.toLocaleDateString(undefined, { timeZoneName: 'long' });

  // Trying to remove date from the string in a locale-agnostic way
  const shortIndex = full.indexOf(short);
  if (shortIndex >= 0) {
    const trimmed = full.substring(0, shortIndex) + full.substring(shortIndex + short.length);
    
    // by this time `trimmed` should be the timezone's name with some punctuation -
    // trim it from both sides
    return trimmed.replace(/^[\s,.\-:;]+|[\s,.\-:;]+$/g, '');

  } else {
    // in some magic case when short representation of date is not present in the long one, just return the long one as a fallback, since it should contain the timezone's name
    return full;
  }
}

console.log(getTimezoneName());
4) thay vì tên. Nhưng tôi nghĩ nó vẫn có thể đọc được như một dự phòng.

P.S. Sẽ không hoạt động trong IE11, giống như giải pháp

function getTimezoneName() {
  const today = new Date();
  const short = today.toLocaleDateString(undefined);
  const full = today.toLocaleDateString(undefined, { timeZoneName: 'long' });

  // Trying to remove date from the string in a locale-agnostic way
  const shortIndex = full.indexOf(short);
  if (shortIndex >= 0) {
    const trimmed = full.substring(0, shortIndex) + full.substring(shortIndex + short.length);
    
    // by this time `trimmed` should be the timezone's name with some punctuation -
    // trim it from both sides
    return trimmed.replace(/^[\s,.\-:;]+|[\s,.\-:;]+$/g, '');

  } else {
    // in some magic case when short representation of date is not present in the long one, just return the long one as a fallback, since it should contain the timezone's name
    return full;
  }
}

console.log(getTimezoneName());
5.

Đã trả lời ngày 7 tháng 6 năm 2019 lúc 7:49Jun 7, 2019 at 7:49

1

Một khả năng ngắn cho kết quả trong ngôn ngữ của người dùng hiện tại:

console.log(new Date().toLocaleDateString(undefined, {day:'2-digit',timeZoneName: 'long' }).substring(4));

Đã trả lời ngày 21 tháng 6 năm 2021 lúc 9:25Jun 21, 2021 at 9:25

Hướng dẫn get timezone name javascript - lấy tên múi giờ javascript

Maeurermamaeurer

1511 Huy hiệu bạc2 Huy hiệu đồng1 silver badge2 bronze badges

1

Nếu bạn đã sử dụng moment.js, bạn có thể đoán tên múi giờ:

moment.tz.guess(); // eg. "America/New York"

Hướng dẫn get timezone name javascript - lấy tên múi giờ javascript

Joel Mellon

3,4451 Huy hiệu vàng25 Huy hiệu bạc21 Huy hiệu đồng1 gold badge25 silver badges21 bronze badges

Đã trả lời ngày 28 tháng 11 năm 2019 lúc 13:40Nov 28, 2019 at 13:40

Omer OmerOmer

7821 Huy hiệu vàng8 Huy hiệu bạc10 Huy hiệu đồng1 gold badge8 silver badges10 bronze badges

3

Bạn có thể sử dụng tập lệnh này. http://pellepim.bitbucket.org/jstz/

Kho lưu trữ nĩa hoặc bản sao ở đây. https://bitbucket.org/pellepim/jstimezonedetect

Khi bạn bao gồm tập lệnh, bạn có thể nhận được danh sách thời gian trong biến -

function getTimezoneName() {
  const today = new Date();
  const short = today.toLocaleDateString(undefined);
  const full = today.toLocaleDateString(undefined, { timeZoneName: 'long' });

  // Trying to remove date from the string in a locale-agnostic way
  const shortIndex = full.indexOf(short);
  if (shortIndex >= 0) {
    const trimmed = full.substring(0, shortIndex) + full.substring(shortIndex + short.length);
    
    // by this time `trimmed` should be the timezone's name with some punctuation -
    // trim it from both sides
    return trimmed.replace(/^[\s,.\-:;]+|[\s,.\-:;]+$/g, '');

  } else {
    // in some magic case when short representation of date is not present in the long one, just return the long one as a fallback, since it should contain the timezone's name
    return full;
  }
}

console.log(getTimezoneName());
6.

Và mã sau được sử dụng để xác định múi giờ của Trình duyệt khách.

var tz = jstz.determine();
tz.name();

Thưởng thức JSTZ!

Đã trả lời ngày 17 tháng 4 năm 2015 lúc 2:41Apr 17, 2015 at 2:41

Seanseansean

1.6041 Huy hiệu vàng15 Huy hiệu bạc13 Huy hiệu đồng1 gold badge15 silver badges13 bronze badges

console.log(new Date().toLocaleDateString(undefined, {day:'2-digit',timeZoneName: 'long' }).substring(4).match(/\b(\w)/g).join(''))

Hướng dẫn get timezone name javascript - lấy tên múi giờ javascript

Eric Aya

69.1K35 Huy hiệu vàng176 Huy hiệu bạc247 Huy hiệu đồng35 gold badges176 silver badges247 bronze badges

Đã trả lời ngày 9 tháng 8 lúc 14:47Aug 9 at 14:47

Hướng dẫn get timezone name javascript - lấy tên múi giờ javascript

Để phát hiện một cái gì đó như "America/New York.", Bạn có thể sử dụng

function getTimezoneName() {
  const today = new Date();
  const short = today.toLocaleDateString(undefined);
  const full = today.toLocaleDateString(undefined, { timeZoneName: 'long' });

  // Trying to remove date from the string in a locale-agnostic way
  const shortIndex = full.indexOf(short);
  if (shortIndex >= 0) {
    const trimmed = full.substring(0, shortIndex) + full.substring(shortIndex + short.length);
    
    // by this time `trimmed` should be the timezone's name with some punctuation -
    // trim it from both sides
    return trimmed.replace(/^[\s,.\-:;]+|[\s,.\-:;]+$/g, '');

  } else {
    // in some magic case when short representation of date is not present in the long one, just return the long one as a fallback, since it should contain the timezone's name
    return full;
  }
}

console.log(getTimezoneName());
7 từ Thư viện Luxon.

import { LocalZone } from 'luxon';

const zoneName = new LocalZone().name;

Đã trả lời ngày 27 tháng 10 năm 2021 lúc 8:13Oct 27, 2021 at 8:13

Hướng dẫn get timezone name javascript - lấy tên múi giờ javascript

Hugo Sohmhugo SohmHugo Sohm

2.4792 Huy hiệu vàng20 Huy hiệu bạc36 Huy hiệu Đồng2 gold badges20 silver badges36 bronze badges

Điều này nhận được mã timezone (ví dụ:

function getTimezoneName() {
  const today = new Date();
  const short = today.toLocaleDateString(undefined);
  const full = today.toLocaleDateString(undefined, { timeZoneName: 'long' });

  // Trying to remove date from the string in a locale-agnostic way
  const shortIndex = full.indexOf(short);
  if (shortIndex >= 0) {
    const trimmed = full.substring(0, shortIndex) + full.substring(shortIndex + short.length);
    
    // by this time `trimmed` should be the timezone's name with some punctuation -
    // trim it from both sides
    return trimmed.replace(/^[\s,.\-:;]+|[\s,.\-:;]+$/g, '');

  } else {
    // in some magic case when short representation of date is not present in the long one, just return the long one as a fallback, since it should contain the timezone's name
    return full;
  }
}

console.log(getTimezoneName());
8) trong JavaScript cũ hơn (tôi đang sử dụng tập lệnh ứng dụng Google với động cơ cũ):

function getTimezoneName() {
  return new Date().toString().get(/\((.+)\)/);
}

Tôi chỉ đặt cái này ở đây trong trường hợp ai đó cần nó.

Đã trả lời ngày 16 tháng 3 năm 2020 lúc 0:30Mar 16, 2020 at 0:30

Toddmotoddmotoddmo

19.2K13 Huy hiệu vàng89 Huy hiệu bạc101 Huy hiệu đồng13 gold badges89 silver badges101 bronze badges

1

Trong javaScript, phương thức date.getTimezOffset () trả về phần bù vùng thời gian từ UTC, trong vài phút, cho địa phương hiện tại.

var x = new Date();
var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;

Moment'timezone sẽ là một công cụ hữu ích. http://momentjs.com/timezone/

Chuyển đổi ngày giữa thời gian

var newYork    = moment.tz("2014-06-01 12:00", "America/New_York");
var losAngeles = newYork.clone().tz("America/Los_Angeles");
var london     = newYork.clone().tz("Europe/London");

newYork.format();    // 2014-06-01T12:00:00-04:00
losAngeles.format(); // 2014-06-01T09:00:00-07:00
london.format();     // 2014-06-01T17:00:00+01:00

Đã trả lời ngày 17 tháng 4 năm 2015 lúc 16:38Apr 17, 2015 at 16:38

2