在 JQuery datetimepicker 中设置日期和时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7164646/
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 date and time in JQuery datetimepicker
提问by Mr Morgan
I have a situation where I'm trying to use Trent Richardson's datetimepicker JQuery extensionto have users select dates and times in one go.
我有一种情况,我试图使用 Trent Richardson 的datetimepicker JQuery 扩展让用户一次性选择日期和时间。
But before that, I want to set the date and time so that the datetimepicker's input box displays a default date and time which would be set from the server. A user would then click inside the input box to call datetimepicker, then the default date and time would appear on datetimepicker. The user can then override these if necessary.
但在此之前,我想设置日期和时间,以便日期时间选择器的输入框显示将从服务器设置的默认日期和时间。然后用户将在输入框内单击以调用 datetimepicker,然后默认日期和时间将出现在 datetimepicker 上。如果需要,用户然后可以覆盖这些。
So far, in code of mine I have:
到目前为止,在我的代码中,我有:
<script type="text/javascript">
$(function(){
$('.example-container > pre').each(function(i){
eval($(this).text());
});
$('#example1').datetimepicker('setDate', (new Date(2010, 11, 20, 16, 03)));
});
</script>
Which will set the date correctly but not the time and hour and minute sliders. The time also displays as 00 03 rather than 16 03.
这将正确设置日期,但不会正确设置时间和小时和分钟滑块。时间也显示为 00 03 而不是 16 03。
Can anyone tell me what I'm doing wrong or not doing here so that datetimepicker shows correct default settings?
谁能告诉我我在这里做错了什么或没有做什么,以便 datetimepicker 显示正确的默认设置?
Other settings are:
其他设置是:
$('#example1').datetimepicker({
dateFormat: 'dd mm yy',
timeFormat: 'hh mm',
showMinute: false,
showSecond: false,
separator: ' ',
});
Thanks
谢谢
回答by mosabusan
I know this issue is old. But for those still looking for a solution here is how I did it. DatetimePicker plugin is an addon to jQueryUI DatePicker, so I think all options and methods of DatePicker still apply in addition to DatetimePicker ones.
我知道这个问题很老了。但对于那些仍在寻找解决方案的人来说,我就是这样做的。DatetimePicker 插件是 jQueryUI DatePicker 的插件,所以我认为 DatePicker 的所有选项和方法除了 DatetimePicker 之外仍然适用。
So if you have a text box with id "dtp" and you want to initialize it with "5/31/2013 7:30 PM", you will do:
因此,如果您有一个 ID 为“dtp”的文本框,并且您想用“5/31/2013 7:30 PM”对其进行初始化,您将执行以下操作:
$("#dtp").datetimepicker({
defaultDate: "5/31/2013", // this is from jQueryUI datepicker
hour: 19,
minute: 30
});
This should highlight the correct date in the calendar and set the sliders to the correct hour and minute as defined in the options.
这应该突出显示日历中的正确日期,并将滑块设置为选项中定义的正确小时和分钟。
Hope this helps.
希望这可以帮助。
回答by BraveNewMath
if you are talking about datetimepicker from Trent Richardson, have a look at this thread https://stackoverflow.com/a/12645192/551811
如果您正在谈论 Trent Richardson 的 datetimepicker,请查看此线程 https://stackoverflow.com/a/12645192/551811
In my experience, even when you have a date prefilled in the textbox, if you want to close the datepicker dialog without entering a date, it replaces the value in the textbox with today's. The solution is to set your textbox value in the onClose method. In this example, value will contain your original value:
根据我的经验,即使您在文本框中预填充了日期,如果您想在不输入日期的情况下关闭日期选择器对话框,它也会将文本框中的值替换为今天的值。解决方案是在 onClose 方法中设置您的文本框值。在此示例中, value 将包含您的原始值:
$('input.datetime').datetimepicker({
onClose: function (value) {
$('input.datetime').val(value);
}
});
回答by Blazemonger
If you pre-fill the input field with a date value, datepicker shoulddefault to that value.
如果您使用日期值预填充输入字段,则日期选择器应默认为该值。
From a usability standpoint, I would use this approach rather than a more programmatic solution.
从可用性的角度来看,我会使用这种方法而不是更具程序性的解决方案。
回答by Mr Morgan
The issue is to do with the customisations of the date and time formats you are using. If you restore the original settings, the display of datetimepicker should be correct.
问题与您使用的日期和时间格式的自定义有关。如果恢复原来的设置,datetimepicker的显示应该是正确的。