[英]MomentJS convert from UTC to desired timezone, not just local
I am using momentjs but having an issue trying to convert a UTC time to a specific timezone (not necessarily local to the current user) that is specified by name 'America/New_York'
.我正在使用 momentjs,但在尝试将 UTC 时间转换为由名称
'America/New_York'
指定的特定时区(不一定是当前用户本地)时遇到问题。 This SO question is similar but didn't really help. 这个问题很相似,但并没有真正的帮助。
My thought process is to create a utc moment obj with the received date from the server and then format that UTC time to the specific timezone for display purposes.我的思考过程是使用从服务器接收到的日期创建一个 utc 时刻 obj,然后将该 UTC 时间格式化为特定时区以进行显示。 A small snippet of how I'm currently approaching this:
我目前如何处理这个问题的一小段:
var cutoffString = '20170421 16:30:00'; // in utc
var utcCutoff = moment.tz(cutoffString, 'YYYYMMDD HH:mm:ss', '+00:00');
var displayCutoff =
moment.tz(utcCutoff.format('YYYYMMDD HH:mm:ss'), 'YYYYMMDD HH:mm:ss', 'America/New_York');
console.log('utcCutoff:', utcCutoff.format('YYYYMMDD hh:mm:ssa Z')); // => utcCutoff: 20170421 04:30:00pm +00:00
console.log('displayCutoff:', displayCutoff.format('YYYYMMDD hh:mm:ssa Z')); // => displayCutoff: 20170421 04:30:00pm +00:00
My assumption here is that displayCutoff would be the utcCutoff time displayed in 'America/New_York' time.我在这里的假设是 displayCutoff 将是 'America/New_York' 时间显示的 utcCutoff 时间。 But it currently is displays the same time as the utcCutoff object.
但它目前与 utcCutoff 对象显示相同的时间。 I also should mention that using
.utc()
instead of .tz
and trying to manipulate the timezone after applying .local()
did not work either.我还应该提到使用
.utc()
而不是.tz
并在应用.local()
后尝试操纵时区也不起作用。
Any help/guidance would be appreciated.任何帮助/指导将不胜感激。
You can use moment.utc
since your input is an UTC string. 您可以使用
moment.utc
因为您的输入是UTC字符串。 You can use tz
to convert your moment object to a given timezone. 您可以使用
tz
将您的时刻对象转换为给定时区。
Please note that the tz
function converts moment object to a given zone, while you are using moment.tz
parsing function that builds a new moment object with the given zone. 请注意,
tz
函数将moment对象转换为给定区域,同时使用moment.tz
解析函数,该函数使用给定区域构建新的时刻对象。 When you do: 当你这样做时:
var displayCutoff =
moment.tz(utcCutoff.format('YYYYMMDD HH:mm:ss'), 'YYYYMMDD HH:mm:ss', 'America/New_York');
you are not converting utcCutoff
to 'America/New_York'
but you are building a new moment object for 20170421 16:30:00
in New York. 你没有将
utcCutoff
转换为'America/New_York'
但是你正在为纽约的20170421 16:30:00
建立一个新的时刻对象。
Here an updated version of your code: 这是您的代码的更新版本:
var cutoffString = '20170421 16:30:00'; // in utc var utcCutoff = moment.utc(cutoffString, 'YYYYMMDD HH:mm:ss'); var displayCutoff = utcCutoff.clone().tz('America/New_York'); console.log('utcCutoff:', utcCutoff.format('YYYYMMDD hh:mm:ssa Z')); // => utcCutoff: 20170421 04:30:00pm +00:00 console.log('displayCutoff:', displayCutoff.format('YYYYMMDD hh:mm:ssa Z')); // => displayCutoff: 20170421 12:30:00pm -04:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.11/moment-timezone-with-data-2010-2020.min.js"></script>
Moment timezone plugin is exactly what you need : http://momentjs.com/timezone/ Moment timezone插件正是您所需要的: http : //momentjs.com/timezone/
var dec = moment("2014-12-01T12:00:00Z");
dec.tz('America/New_York').format('ha z'); // 5am PDT
With momentjs-timezone
you can convert from any timezone to any other timezone.使用
momentjs-timezone
您可以将任何时区转换为任何其他时区。
You need to specify the start time zone and before formatting the target timezone.您需要在格式化目标时区之前指定开始时区。
Here is an example which converts a date time from UTC to three other timezones:这是一个将日期时间从 UTC 转换为其他三个时区的示例:
const moment = require('moment-timezone')
const start = moment.tz("2021-12-08T10:00:00", "UTC") // original timezone
console.log(start.tz("America/Los_Angeles").format())
console.log(start.tz("Asia/Calcutta").format())
console.log(start.tz("Canada/Eastern").format())
This will print out:这将打印出:
2021-12-08T02:00:00-08:00
2021-12-08T15:30:00+05:30
2021-12-08T05:00:00-05:00
Instead of UTC as start timezone you can use any other timezone too, like "Asia/Seoul" and obviously get different results with the same script:除了 UTC 作为开始时区,您也可以使用任何其他时区,例如“亚洲/首尔”,显然使用相同的脚本会得到不同的结果:
const moment = require('moment-timezone')
const start = moment.tz("2021-12-08T10:00:00", "Asia/Seoul")
console.log(start.tz("America/Los_Angeles").format())
console.log(start.tz("Asia/Calcutta").format())
console.log(start.tz("Canada/Eastern").format())
This prints out:这打印出来:
2021-12-07T17:00:00-08:00
2021-12-08T06:30:00+05:30
2021-12-07T20:00:00-05:00
All momentjs timezones are listed here:此处列出了所有 momentjs 时区:
https://gist.github.com/diogocapela/12c6617fc87607d11fd62d2a4f42b02a https://gist.github.com/diogocapela/12c6617fc87607d11fd62d2a4f42b02a
There is no need to use MomentJs to convert your timezone to specific timezone.
无需使用MomentJs将您的时区转换为特定时区。 Just follow my given below code, it will work for you :
只需按照下面给出的代码,它将适用于您:
$(document).ready(function() { //EST setInterval( function() { var estTime = new Date(); var currentDateTimeCentralTimeZone = new Date(estTime.toLocaleString('en-US', { timeZone: 'America/Chicago' })); var seconds = currentDateTimeCentralTimeZone.getSeconds(); var minutes = currentDateTimeCentralTimeZone.getMinutes(); var hours = currentDateTimeCentralTimeZone.getHours();//new Date().getHours(); var am_pm = currentDateTimeCentralTimeZone.getHours() >= 12 ? "PM" : "AM"; if (hours < 10){ hours = "0" + hours; } if (minutes < 10){ minutes = "0" + minutes; } if (seconds < 10){ seconds = "0" + seconds; } var mid='PM'; if(hours==0){ //At 00 hours we need to show 12 am hours=12; } else if(hours>12) { hours=hours%12; mid='AM'; } var x3 = hours+':'+minutes+':'+seconds +' '+am_pm // Add a leading zero to seconds value $("#sec").html(x3); },1000); });
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </head> <body> <p class="date_time"><strong id="sec"></strong> </p> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.