2
votes

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.com/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.

2

2 Answers

3
votes

I found that one of the commonly used ones at https://github.com/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.com/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()
0
votes

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