JQuery Datepicker - how can I format a date as an epoch timestamp (in seconds, not milliseconds)

I use the jquery datepicker plugin to set the date field, which is stored as an epoch timestamp in db (the field, publish_time, maps directly to the table schema).

Datepicker seems to only support an era in milliseconds, not seconds. Compounding it is that it supports milli and 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 : '@' }); 

Literature:
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); } }); 
+6
jquery datepicker epoch
source share
2 answers

Use the millisecond view and parseInt to get an integer value. Then you can multiply by 1000 to get seconds. This requires a little processing instead of directly using the datepicker value.

+2
source share

I am typing this as an answer, not a comment, so they do not miss it: I caution against using the raw timestamp from your client as a value to fill in your database, if you are not really sure that this is what you want to do. Your clients may be in different time zones than your servers (in fact, in general, they have an approximately 96% chance :-) therefore, when they select April 2, this may or may not end as " April 2 "in your database.

Sometimes, of course, you want client time, but in my experience this is not too often.

+6
source share

All Articles