Search code examples
jquerydatepickerepoch

jQuery Datepicker - How can I format the date as an epoch timestamp (in seconds NOT milliseconds)


I'm using the jquery datepicker plugin to set a date field that is stored as an epoch timestamp in the db (the field, publish_time, maps directly to the table schema).

It seems that Datepicker only supports epoch in milliseconds, and not seconds. Its aggravating that it supports milli & nano seconds, but not seconds.

Are there any quick workarounds?

// Setup datepicker
$('[name=datepicker-publish_time]').datepicker({
    dateFormat : 'mm-dd-yy',
    altField : '[name=publish_time]',
     altFormat : '@'
});

References:
jQuery Datepicker - http://jqueryui.com/demos/datepicker/#option-defaultDate
jQuery Support Date Formats - http://docs.jquery.com/UI/Datepicker/formatDate

Edit: Below is a quick dirty solution...

$('[name=datepicker-publish_time]').datepicker({
    dateFormat : 'mm-dd-yy',
    onSelect : function(dateText, inst)
    {
        var epoch = $.datepicker.formatDate('@', $(this).datepicker('getDate')) / 1000;

        $('[name=publish_time]').val(epoch);
    }
});

Solution

  • Use the millisecond representation and parseInt to get an integer value. Then you can multiply with 1000 to get the seconds. This just requires a little processing instead of taking the value of the datepicker directly.