获取特定时区的日期 [英] Get date in a specific timezone

查看:48
本文介绍了获取特定时区的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不住在太平洋地区,我正在寻找一种方法来获取太平洋地区的当前日期,因为我的午夜与太平洋午夜不匹配,该怎么办?

i don't live in the pacific area and i am looking for a way to get the current day in the pacific area as my midnight does not match pacific midnight, how to do?

当前代码

//how to set this to Pacific Time?
var timezone = new Date();

//get the current day
var day = timezone.getDate();

//use the day           
var row_for_day = day+3;

推荐答案

这是一种方法,使用 Date#toLocaleString():

Here's one way to do this, using Date#toLocaleString():

(目标时区名称可以在这里查询:https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)

(target timezone name can be looked up here: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)

const target = "America/Los_Angeles";

const laDate = +new Date().toLocaleString("en-US", {
  timeZone: target,  // conversion here
  day: "numeric"     // return day only
});

console.log("date in LA:", laDate);

我一直在寻找现有的答案,但找不到合适的、最新的、简洁的答案.指出一个,我会删除它.

I was looking for existing answers but wasn't able to find a good, current, concise fit. Point one out and I will remove this.

这篇关于获取特定时区的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆