不同语言环境和时区的 Javascript 日期对象

2023-06-14前端开发问题
9

本文介绍了不同语言环境和时区的 Javascript 日期对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我需要编写一个 Web 应用程序来显示不同地区的人们的事件.我差不多完成了,但是日期有两个问题:

I need to write a web application that show events of people in different locale. I almost finished it, but there're 2 problems with date:

  • 使用日期 javascript 对象,日期取决于用户计算机设置,并不可靠
  • 如果在具有不同时区的地方发生事件尊重用户当前位置,我必须在 () 内打印它.是否可以在 javascript 中构建具有给定时区和日光设置的日期对象?
  • using date javascript object, the date depends on user computer settings and it's not reliable
  • if there's an event in a place with dfferent timezone respect user current position, i have to print it inside (). Is it possible in javascript to build a date object with a given timezone and daylight settings?

我还找到了一些解决方法,例如 jsdate 和 date webservices,但它们并没有解决具有正确时区和日光设置的 javascript 对象的问题(用于日期操作,例如添加天数等).

I also find some workaround, such as jsdate and date webservices, but they don't overcome the problem of having a javascript object with the correct timezone and daylight settings (for date operation such as adding days and so on).

推荐答案

有几点要记住.

以 UTC 时间存储所有事件日期时间

是的,没有办法解决这个问题.

Yes, there is no getting around this.

找出所有时区...

...系统中的所有用户.您可以使用以下检测脚本:http://site.pageloom.com/automatic-timezone-detection-with-javascript.它会给你一个时区键,例如America/Phoenix".

...of all the users in the system. You can use the following detection script: http://site.pageloom.com/automatic-timezone-detection-with-javascript. It will hand you a timezone key such as for example "America/Phoenix".

在您的情况下,您需要将时区与事件一起存储,因为用户可能会切换时区 - 但事件将始终在特定时区发生.(啊)

In your case you need to store the timezone together with the event, since a user may switch timezone - but the event will always have happened in a specific one. (argh)

选择您的显示机制

如果您想使用 Javascript 本地化您的活动日期,也有一个漂亮的库(可以使用上一个脚本提供的键).这里:https://github.com/mde/timezone-js.

If you want to localize your event dates with Javascript, there is a nifty library for that too (which can use the keys supplied with the previous script). Here: https://github.com/mde/timezone-js.

使用该库,您可以执行以下操作:

with that library you can for example do this:

var dt = new timezoneJS.Date(UTC_TIMESTAMP, 'America/New_York');

var dt = new timezoneJS.Date(2006, 9, 29, 1, 59, 'America/Los_Angeles');

例如,UTC_TIMESTAMP 可以是 1193855400000.America/New_York是您在事件发生时检测到的时区.

where UTC_TIMESTAMP for example could be 1193855400000. And America/New_Yorkis the timezone you have detected when the event took place.

您从中获得的 dt 对象将表现为普通的 JavaScript Date 对象.但会自动更正"到您指定的时区(包括 DST).

The dt object that you get from this will behave as a normal JavaScript Date object. But will automatically "correct" itself to the timezone you have specified (including DST).

如果您愿意,您可以在提供页面之前在后端进行所有更正.因为我不知道你在那里使用什么编程语言,所以我不能给你任何直接的提示.但基本上它遵循相同的逻辑,如果您知道时区和 UTC 日期时间 -> 您可以本地化日期时间.所有编程语言都有相应的库.

If you want to, you can do all the corrections in the backend - before you serve the page. Since I don't know what programming language you are using there, I cannot give you any immediate tips. But basically it follows the same logic, if you know the timezone, and the UTC datetime -> you can localize the datetime. All programming languages have libraries for that.

这篇关于不同语言环境和时区的 Javascript 日期对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Fatal error: Call to a member function fetch_assoc() on a no
业务场景:使用update语句去更新数据库字段。 原因:update接收值不正确。原来代码: $query = "UPDATE student SET date = now() WHERE id = $id";$result = $mysqli-query($query2) or die($mysqli-error); // 问题出现了在这句 $data = $result-fetch_ass...
2024-12-13 前端开发问题
136

layui实现laydate日历控件控制之前日期不可选择
具体实现代码如下: laydate.render({ elem: '#start_time', min:0, //,type: 'date' //默认,可不填}); 只要加一个min参数,就可以控制了。0表示之前的日期不可...
2024-11-29 前端开发问题
133

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313