Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
    }
});
like image 982
John Himmelman Avatar asked Feb 23 '10 20:02

John Himmelman


1 Answers

I'm typing this as an answer and not a comment so it doesn't get missed: I caution strongly against using the raw timestamp from your client as a value to stuff into your database unless you're really sure that that's what you want to do. Your clients may be in different time zones than your servers (in fact, in general there's roughly a 96% chance they are :-) so when they pick "April 2" it may or may not end up as "April 2" in your database.

Sometimes of course you want the client time, but in my experience that's not too common.

like image 112
Pointy Avatar answered Sep 20 '22 16:09

Pointy