Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Datepicker unix timestamp

I add a datepicker with jQuery datepicker and make use of the altFormat '@' --> see http://docs.jquery.com/UI/Datepicker/formatDate

 // Function datepicker
        $("#obsDate").datepicker({
            altField: '#actualDate',
            altFormat: '@',        // Gives a timestamp dateformat
            dateFormat: "dd-mm-yy",
            showOn: "button",
            buttonImage: $("#datePickerImg").val(),
            buttonImageOnly: true,
        });

When the user picks a value the unix timestamp is set. Like : 1312840800000

This is in miliseconds so i id do /1000

But when i convert the timestamp with the function in C#

private static DateTime ConvertFromUnixTimestamp(double timestamp)
        {
            var origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return origin.AddSeconds(timestamp);
        }

I get always one day ealier..

What i'm doing wrong?

UPDATED: When i use the build in function of javascript gettime()

var ts = Math.round((new Date().getTime() / 1000));

I get the right timestamp...

  • Example with getTime() i get: 30-08-2011 --> 1314628036

Example with the datepicker i get : 29-08-2011 --> 1314568800.

This is also with ticks (!) in the datepicker.

like image 439
amernov Avatar asked Aug 29 '11 13:08

amernov


1 Answers

This obviously IS a timezone problem.

getTime()
This function returns milliseconds since 'epoch', meaning you get Unix timestamp * 1000 as seen from local computer. See If javascript “(new Date()).getTime()” is run from 2 different Timezones.

datepicker({altFormat: '@'})
From what I see in jQuery library, datepicker internally uses formatDate function, that takes timezone into account (I started here: jQuery.datepicker.formatDate and timezone offset...)

So, on my PC I get 2 hours difference. I can't think of an easy way to resolve this, but you could try the following: datetimepicker getDate to return Date / Time in UTC format

like image 171
OzrenTkalcecKrznaric Avatar answered Oct 15 '22 13:10

OzrenTkalcecKrznaric