jQuery 在 bootstrap datetimepicker 输入中设置日期

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

Set date in bootstrap datetimepicker input

jquerytwitter-bootstrap

提问by Snake Eyes

I used the datetimepicker from: http://eonasdan.github.io/bootstrap-datetimepicker/Installing/

我使用了日期时间选择器:http: //eonasdan.github.io/bootstrap-datetimepicker/Installing/

There no provide how to use function (date, show, hide, etc) so I don't know how to set the value for input using datetimepicker using datefunction which says:

没有提供如何使用函数(日期、显示、隐藏等),所以我不知道如何使用 datetimepicker 使用date函数设置输入值,它说:

date([newDate])

Takes string, Date, moment, null parameter and sets the components model current moment to it.

日期([新日期])

获取字符串、日期、时刻、空参数并将组件模型当前时刻设置为它。

I tried

我试过

$('#datetimepicker2').datetimepicker(
    date: function() { return new Date(1434544649384); }
);

Where 1434544649384is a timestamp.

1434544649384时间戳在哪里。

But it doesn't work and doesn't update input text/value...

但它不起作用并且不更新输入文本/值...

JsFiddle: http://jsfiddle.net/0Ltv25o8/1397/

JsFiddle:http: //jsfiddle.net/0Ltv25o8/1397/

回答by xfdai

quote from http://eonasdan.github.io/bootstrap-datetimepicker/Functions/

引自http://eonasdan.github.io/bootstrap-datetimepicker/Functions/

Note All functions are accessed via the data attribute e.g. $('#datetimepicker').data("DateTimePicker").FUNCTION()

So you can update date like following:

所以你可以像下面这样更新日期:

$('#datetimepicker2').data("DateTimePicker").date(new Date());

回答by Cyril Duchon-Doris

The correct syntax is :

正确的语法是:

$('#datetimepicker2').datetimepicker({
    date: new Date(1434544882775)
});

回答by Sandeep

Had to add, I was able to solve it.

不得不补充,我能够解决它。

$('#datetimepicker1').datetimepicker({});
$('#datetimepicker1').data("DateTimePicker").date(moment(new Date ).format('DD/MM/YYYY HH:mm'));

回答by Pierre Maoui

You want to set a default date value :

您要设置默认日期值:

var d = $('#datetimepicker1').datetimepicker({
    "defaultDate":new Date()
});

http://jsfiddle.net/0Ltv25o8/1399/

http://jsfiddle.net/0Ltv25o8/1399/

回答by mechenbier

If you are looking to set the date of your datetimepicker, use the defaultDateoption. From the documentation:

如果您要设置日期时间选择器的日期,请使用该defaultDate选项。从文档:

defaultDate: Sets the picker default date/time. Default: false. Accepts: date, moment, string.

defaultDate:设置选择器的默认日期/时间。默认值:假。接受:日期、时刻、字符串。

Your options would look like:

您的选项如下:

$('#datetimepicker2').datetimepicker({
    defaultDate: new Date(1434544649384)
});

http://jsfiddle.net/0Ltv25o8/1400/

http://jsfiddle.net/0Ltv25o8/1400/

回答by Jeff Yates

For me although I managed to change the date, it also changed the format incorrectly. When I applied the format separatelyit then worked as expected:

对我来说,虽然我设法更改了日期,但它也错误地更改了格式。当我单独应用格式时,它按预期工作:

var d = $('#datetimepicker1').({ date: moment(), format: $scope.userDateFormatMoment });

回答by Sujit Patil

Use defaultDateoption to set date.

使用defaultDate选项设置日期。

$('#datetimepicker2').datetimepicker({
    defaultDate:  new Date(1434544649384) 
});

回答by Skeets

None of the answers here will work in all situations. I've combined a couple of the answers here to get one that will:

这里的任何答案都不适用于所有情况。我在这里结合了几个答案以获得一个:

var $ele = $("#datetimepicker2");
var date = new Date(1434544649384);
var datePickerObject = $ele.data("DateTimePicker");

if (typeof datePickerObject !== "undefined") {
  // it's already been Initialize . Just update the date.
  datePickerObject.date(date);
}
else {
  // it hasn't been initialized yet. Initialize it with the date.
  $ele.datetimepicker({
    date: date
  });
}

回答by Nathan Willson

xfdai's answer worked for me, except if you're getting

xfdai 的回答对我有用,除非你得到

$('#datetimepicker2').data("DateTimePicker")
= undefined

then try $('#datetimepicker2').data("datetimepicker")(lowercase data param) instead.

然后尝试$('#datetimepicker2').data("datetimepicker")(小写数据参数)代替。

回答by Noel Swanson

Here's what worked for me:

以下是对我有用的内容:

var dateString = '2200-12-31';
var dateParts = dateString.split('-');
var date = new Date(dateParts[0], dateParts[1] - 1, dateParts[2]);
$('#leaseEnd').data('datetimepicker').setDate(date);
$('#leaseEnd').data('datetimepicker').update();

Notes:

笔记:

I had to use the lowercase term 'datetimepicker' - thanks Nathan Wilson

我不得不使用小写术语“datetimepicker” - 感谢 Nathan Wilson

I had to make the string into a date object

我不得不把字符串变成一个日期对象

I had to use the update() function to get the calendar to show the newly selected date

我不得不使用 update() 函数来让日历显示新选择的日期

Don't forget that if you are including hours javascript may modify your update because of timezone difference. In that case include the timezone in your date string.

不要忘记,如果您包含小时数,javascript 可能会因为时区差异而修改您的更新。在这种情况下,请在日期字符串中包含时区。