HTML 输入类型 datetime-local 设置错误的时区

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

本文介绍了HTML 输入类型 datetime-local 设置错误的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我创建了一个应用程序,它接受 HTML 输入并通过 JavaScript 在本机日历事件上创建一个事件.它从 <input type="datetime-local"> 中花费时间,并且由于选择了不同的时区,因此它输入了不同的时间.如果我输入 1 o'clock PM 作为时间,它将返回 8 o'clock AM.

I've created an app that takes in HTML inputs and goes through JavaScript to create an event on a native calendar events. It takes the time from the <input type="datetime-local">, and it's putting in a different time because it's picking a different time zone. If I enter 1 o'clock PM as a time it will return 8 o'clock AM.

<input type="datetime-local" id="startDate" name="startDate">

还有 JavaScript:

And the JavaScript:

var startDate = new Date($("#startDate").val());

任何帮助都会很棒.如果需要,我可以发布更多代码.

Any help would be awesome. I can post more code if needed.

推荐答案

HTML5 datetime-local 输入类型将返回一个 string 值,其中包含日期和 ISO8601 格式的时间,精确到分钟,没有任何时区偏移.

The HTML5 datetime-local input type will give you a string value back, which contains the date and time in ISO8601 format, with minute precision, and without any time zone offset.

例如:2014-07-12T01:00

JavaScript 日期对象在从字符串中解析日期时是出了名的不一致.在大多数实现中,当您提供这样的字符串时,它会错误地假定该值是 UTC.因此,您返回的 Date 对象将根据您本地计算机的时区偏移量进行调整.

The JavaScript date object is notoriously inconsistent when it comes to parsing dates from strings. In most implementations, when you provide a string like this, it erroneously assumes the value is in UTC. Therefore, the Date object you get back will be adjusted by the time zone offset from your local computer.

有两种方法可以解决这个问题:

There are two approaches to work around the problem:

选项 1

将字符串处理为可能被 Date 对象的解析器解释为本地时间的格式.具体来说,将破折号 (-) 替换为正斜杠 (/),并将 T 替换为空格.

Manipulate the string to a format that will likely be interpreted as local time by the Date object's parser. Specifically, replace the dashes (-) with forward slashes (/) and replace the T with a space.

var s = $("#startDate").val();
var startDate = new Date(s.replace(/-/g,'/').replace('T',' '));

选项 2

使用具有更强大的日期解析能力的库.有几种可用.其中最流行的是 moment.js.

Use a library with more capable date parsing abilities. There are several available. One of the most popular is moment.js.

Moment.js 有很多选项,但碰巧默认行为正是您所需要的.因此,您可以将字符串传递给 moment 构造函数而无需任何参数.

Moment.js has lots of options, but it just so happens that the default behavior is exactly what you need. So you can just pass the string to the moment constructor without any parameters.

var s = $("#startDate").val();
var startDate = moment(s).toDate();

这篇关于HTML 输入类型 datetime-local 设置错误的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

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 laydate日期时间范围,时间默认设定为23:59:59
在Layui中,如果你想设置日期时间选择器(datetime)的默认结束时间为当天的23:59:59,你可以使用如下代码: laydate.render({ elem: '#test10' ,type: 'datetime' ,range: true ,max: '{:date("Y-m-d 23:59:59")}' ,ready: function(date){ $(".layui-laydat...
2024-10-24 前端开发问题
279

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125