最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Bootstrap Datetimepicker set Time programmatically from Epoch - Stack Overflow

programmeradmin1浏览0评论

I have a simple Datetimepicker:

$('#deadline_picker').datetimepicker();

Trivially I can produce the utc / or local date string with var x= new Date(timestamp) or var x= (new Date(timestamp)).toUTCString(). And Assign it as a value to the field - $('#deadline_picker').val(x)

But for one the Datetime Picker object does not seem responsive to programmatic changes (if the Calendar has a certain date selected it will still show the same date selected and it does not appear to be timezone conscious). It is very critical for me to be able to set an exact equivalent of the Epoch time programmatically. Supposedly the datetimepicker has API methods, but the docs don't have a single example.

Edit: I'm using the copy that's referenced as being currently maintained:

Edit2: Apparently it has different methods from: / But neither of them seem to have Timezone support.

I have a simple Datetimepicker:

$('#deadline_picker').datetimepicker();

Trivially I can produce the utc / or local date string with var x= new Date(timestamp) or var x= (new Date(timestamp)).toUTCString(). And Assign it as a value to the field - $('#deadline_picker').val(x)

But for one the Datetime Picker object does not seem responsive to programmatic changes (if the Calendar has a certain date selected it will still show the same date selected and it does not appear to be timezone conscious). It is very critical for me to be able to set an exact equivalent of the Epoch time programmatically. Supposedly the datetimepicker has API methods, but the docs don't have a single example.

Edit: I'm using the copy that's referenced as being currently maintained: https://github./Eonasdan/bootstrap-datetimepicker

Edit2: Apparently it has different methods from: http://www.malot.fr/bootstrap-datetimepicker/ But neither of them seem to have Timezone support.

Share Improve this question edited Nov 16, 2019 at 2:43 Cœur 38.8k25 gold badges206 silver badges278 bronze badges asked Jul 15, 2014 at 15:01 user3467349user3467349 3,2015 gold badges40 silver badges64 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

I found that one of the monly used ones at https://github./smalot/bootstrap-datetimepicker will not take (new Date).toUTCString() as an argument and is not helpful. The one I mentioned in my question: https://github./Eonasdan/bootstrap-datetimepicker - Also does not appear to take (new Date()) strings of either kind, but it will take both moment() and moment.utc() objects, so in my case this worked. (Deadline is in a table row represented visually as a Timestring without seconds, year or milliseconds, to preserve datatable space).

$td = $(this).closest('tr').children('td');
var timestamp = moment.utc($td.eq(2).attr('value')*1000);
$('#deadline_picker').data("DateTimePicker").setDate(deadline); 

This can be reversed with:

$('#deadline_picker').data("DateTimePicker").getDate().unix()

Can you try this?

    var x= (new Date(timestamp)).toUTCString();
    $('#deadline_picker').datetimepicker("update", x);

If this didnt work, try this:

    $('#deadline_picker').data({date: x});
    $('#deadline_picker').datetimepicker('update');
    $('#deadline_picker').datetimepicker().children('input').val(x);

SOURCE

发布评论

评论列表(0)

  1. 暂无评论