javascript 从 Date 设置 datetime-local 的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30166338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting value of datetime-local from Date
提问by Benjy Kessler
I would like to set the value of a datetime-local input with the current date and time. Right now I have an ugly solution that involves slicing the first 17 characters. In addition it sets the time in GMT instead of the local time. My code is as follows:
我想用当前日期和时间设置日期时间本地输入的值。现在我有一个丑陋的解决方案,涉及切片前 17 个字符。此外,它以格林威治标准时间而不是本地时间设置时间。我的代码如下:
<input type="datetime-local" name="name" id="1234">
<script type="text/javascript">
var d = new Date();
var elem = document.getElementById("1234");
elem.value = d.toISOString().slice(0,16);
</script>
I have two problems with this code:
这段代码有两个问题:
- Is there a way to convert from a
Dateto a legal value without manually slicing the string? - I would like the string to be displayed in the datetime-local as
DD/MM/YYYY, hh:mm(e.g.05/11/2015, 14:10it is13:10in GMT but I am in GMT+1 so I want to display14:10). What is currently displayed is05/11/2015, 01:10 PM. I would like to remove the PM and display in local time.
- 有没有办法在
Date不手动切片字符串的情况下从 a 转换为合法值? - 我希望字符串在日期时间本地显示为
DD/MM/YYYY, hh:mm(例如,05/11/2015, 14:10它13:10在 GMT 中,但我在 GMT+1 中,所以我想显示14:10)。当前显示的是05/11/2015, 01:10 PM. 我想删除 PM 并在本地时间显示。
This might be an XY problem, so if I am doing it completely wrong and there is a better way to display datetime pickers in html, I would be happy to hear.
这可能是一个XY 问题,所以如果我做的完全错误并且有更好的方法在 html 中显示日期时间选择器,我会很高兴听到。
采纳答案by Bellash
The toISOStringfunction is responsible of converting your local date (new Date) into GMT.
该toISOString函数负责将您的本地日期 ( new Date) 转换为 GMT。
If you don't want to use GMT then slice, you need to use the pure Date constructor and all of the getX functions, where X is (days, month, year...)
如果不想使用 GMT 然后切片,则需要使用纯 Date 构造函数和所有 getX 函数,其中 X 是(天、月、年...)
In addition, you'll need to extend the Numberobject with a function that will help you to return 01instead of 1for example, to preserve the dd/mm/yyyy, hh/mmformat.
此外,您需要Number使用一个函数来扩展对象,该函数将帮助您返回01而不是1例如保留dd/mm/yyyy, hh/mm格式。
Let me call this prototype function AddZero
让我调用这个原型函数 AddZero
<input type="datetime-local" name="name" id="1234">
<script type="text/javascript">
Number.prototype.AddZero= function(b,c){
var l= (String(b|| 10).length - String(this).length)+1;
return l> 0? new Array(l).join(c|| '0')+this : this;
}//to add zero to less than 10,
var d = new Date(),
localDateTime= [(d.getMonth()+1).AddZero(),
d.getDate().AddZero(),
d.getFullYear()].join('/') +', ' +
[d.getHours().AddZero(),
d.getMinutes().AddZero()].join(':');
var elem=document.getElementById("1234");
elem.value = localDateTime;
</script>
回答by neurino
I ended up subtracting getTimezoneOffsetminutes to adjust the toISOStringvalue:
我最终减去getTimezoneOffset分钟来调整toISOString值:
var now = new Date();
now.setMinutes(now.getMinutes() - now.getTimezoneOffset());
document.getElementById('dt').value = now.toISOString().slice(0,16);
<input id="dt" type="datetime-local" />
回答by Harish
Replace this line
替换这一行
elem.value = d.toISOString().slice(0,16);
with
和
elem.value = d.toLocaleString();
This will still print "am/pm" at the end but it takes care of the time adjustment to local values.
这仍然会在最后打印“am/pm”,但它负责将时间调整为本地值。
回答by atmadja
Personally i used :
我个人使用过:
<input type="datetime-local" name="name" id="1234" value="<?php echo date('Y-m-d');echo 'T';echo date (H);echo ':';echo date(i);?>">

