如何通过 jQuery 在 datetime-local 上设置日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15484772/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:10:28  来源:igfitidea点击:

How to set datetime on datetime-local via jQuery

javascriptjqueryhtml

提问by Vlad Bezden

I have datetime-local html control on my form and I need to set date and time on it dynamically via JS or jQuery. How can I do it?

我的表单上有 datetime-local html 控件,我需要通过 JS 或 jQuery 动态设置日期和时间。我该怎么做?

<input type="datetime-local" id="publishDate" required/>

I tried

我试过

$("#publishDate").val("2013-3-18 13:00");
$("#publishDate").val("2013-3-18T13:00");
$("#publishDate").val(new Date().toLocalString());

but nothing worked.

但没有任何效果。

Thanks in advance.

提前致谢。

回答by Claudio Redi

This would do the trick

这可以解决问题

$("#publishDate").val("2013-03-18T13:00");

You need to use 2 digits for the month to make your sample work.

您需要使用 2 位数字表示月份才能使您的示例工作。

回答by Varun Natraaj

If you want to set the current date, then you can try this:

如果你想设置当前日期,那么你可以试试这个:

__Method 1:__

$(document).ready(function(){
    $('input[type=datetime-local]').val(new Date().toJSON().slice(0,19));
});

__Method 2:__

function zeroPadded(val) {
  if (val >= 10)
    return val;
  else
    return '0' + val;
}

$(document).ready(function(){
  d = new Date();
  $('input[type=datetime-local]').val(d.getFullYear()+"-"+zeroPadded(d.getMonth() + 1)+"-"+zeroPadded(d.getDate())+"T"+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());
});

Note:You can replace $('input[type=datetime-local]') with Idor Nameor Classof the datetime-local field.

注意:您可以将 $('input[type=datetime-local]') 替换为 datetime-local 字段的IdNameClass

EDIT: d.getMonth() returns a value between 0-11, so to input the proper month 1 needs to be added to the result.

编辑:d.getMonth() 返回 0-11 之间的值,因此要输入正确的月份,需要将 1 添加到结果中。

回答by klaudyuxxx

What about?

关于什么?

var now=new Date();
console.log(new Date(now.getTime()-now.getTimezoneOffset()*60000).toISOString().substring(0,19));

回答by rb-

I wrote a jQuery method that sets the current UTC time. It's useful for initializing datetimeand datetime-localinput fields.

我写了一个jQuery 方法来设置当前的 UTC 时间。它对于初始化datetimedatetime-local输入字段很有用。

Here's how you would use it to initialize a field.

以下是您将如何使用它来初始化字段。

$('input[type="datetime"]').setNow();

Or pass an argument of trueto only set fields with no value.

或者传递一个参数trueto 只设置没有值的字段。

$('input[type="datetime"]').setNow(true);

回答by Twinstar

Here's a solution in formatting the date using momentjs.
This will get the current date and time

这是使用momentjs格式化日期的解决方案。
这将获得当前日期和时间

moment().format('YYYY-MM-DDThh:mm:ss.SSS')

回答by jgmjgm

This component is messed up because it's not specified properly yet. Implementations are likely to be quirky as well.

这个组件搞砸了,因为它还没有正确指定。实现也可能是古怪的。

The right way to do it should be to pass a date object, with JS and DOM it would make no sense to not have this. Doing things with string manipulation is going to invoke Zalgo. Sooner or later it will break with the locale or timezone.

正确的做法应该是传递一个日期对象,使用 JS 和 DOM 没有这个是没有意义的。处理字符串操作将调用 Zalgo。迟早它会与语言环境或时区中断。

I have looked for something like this and in Chrome 46 found:

我一直在寻找这样的东西,并在 Chrome 46 中找到:

$('input[type=datetime-local]').prop('valueAsNumber', Math.floor(new Date() / 60000) * 60000); // 60seconds * 1000milliseconds

If you don't remove the second and milliseconds they will show in the input field.

如果您不删除秒和毫秒,它们将显示在输入字段中。

There is a valueAsDate property as well but mysteriously:

还有一个 valueAsDate 属性,但很神秘:

Uncaught DOMException: Failed to set the 'valueAsDate' property on 'HTMLInputElement': This input element does not support Date values.

So they haven't finished implementing it yet or choose a bad name for that property (it shows as null even when something is set though).

所以他们还没有完成它的实现,或者为该属性选择了一个错误的名称(即使设置了某些内容,它也会显示为 null)。