Javascript jQuery Datepicker unix 时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7230954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:25:04  来源:igfitidea点击:

jQuery Datepicker unix timestamp

javascriptjquerydatepicker

提问by amernov

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

我添加了一个带有 jQ​​uery datepicker 的日期选择器并使用 altFormat '@' --> 参见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

当用户选择一个值时,unix 时间戳被设置。喜欢:1312840800000

This is in miliseconds so i id do /1000

这是以毫秒为单位,所以我是 /1000

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

但是当我用 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()

更新:当我使用 javascript gettime() 的内置函数时

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

I get the right timestamp...

我得到了正确的时间戳...

  • Example with getTime() i get: 30-08-2011 --> 1314628036
  • 我得到的 getTime() 示例:30-08-2011 --> 1314628036

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

我得到的日期选择器示例:29-08-2011 --> 1314568800。

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

这也是日期选择器中的刻度 (!)。

回答by OzrenTkalcecKrznaric

This obviously ISa timezone problem.

这显然一个时区问题。

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

getTime()
此函数返回自“纪元”以来的毫秒数,这意味着您可以Unix timestamp * 1000从本地计算机上看到。请参阅是否从 2 个不同的时区运行 javascript“(new Date()).getTime()”

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

datepicker({altFormat: '@'})
从我在jQuery库中看到的,datepicker内部使用的formatDate函数,考虑了时区(我从这里开始: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

所以,在我的电脑上,我得到了 2 小时的差异。我想不出一个简单的方法来解决这个问题,但您可以尝试以下操作: datetimepicker getDate 以 UTC 格式返回日期/时间

回答by Alexander Volkov

In relation to your code you must do next:

关于您的代码,您必须执行以下操作:

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

This is because Jan 1th in most of the timezones is correspond to a unixtime value that is greater than zero or one. So, more correctly you must set (not add) the timestamp to the Datetimeobject (if you have a setter).

这是因为大多数时区中的 1 月 1 日对应于大于零或一的 unixtime 值。因此,更准确地说,您必须为Datetime对象设置(而不是添加)时间戳(如果您有一个 setter)。

回答by Robert Paulson

Round Trip Precision

往返精度

Unix times (milliseconds since 1 Jan 1970) are less precise than .Net DateTime's so for round-trip to .Net code, or if comparing to a .Net DateTime, this is the precision to expect. E.g. .Net -> Unix -> .Net can be off by this amount.

Unix 时间(自 1970 年 1 月 1 日以来的毫秒数)不如 .Net DateTime 精确,因此对于到 .Net 代码的往返,或者如果与 .Net DateTime 相比,这是预期的精度。例如 .Net -> Unix -> .Net 可以关闭这个数量。

var unixEpochPrecision = new TimeSpan(TimeSpan.TicksPerMillisecond);

It might not really matter, but you should at least be aware of it.

这可能并不重要,但您至少应该意识到这一点。

Convert to Unix Epoch milliseconds

转换为 Unix Epoch 毫秒

UTC dates are required when you start, or you will otherwise be off by a few hours. This makes sense because Unix times, irrespective of time zone, are always expressed in UTC. If only .net was that way.

开始时需要 UTC 日期,否则您将离开几个小时。这是有道理的,因为 Unix 时间,无论时区如何,总是以 UTC 表示。如果只有 .net 是那样的话。

DateTime UnixEpochBaseDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

Int64 ToUnixTime(DateTime utcValue)
{
    if (utcValue.Kind != DateTimeKind.Utc)
        throw new ArgumentException("Value must be in Utc.");

    var result = (utcValue - UnixEpochBaseDate).Ticks / TimeSpan.TicksPerMillisecond;
    return result;
} 

Convert from Unix Epoch to CLR DateTime

从 Unix Epoch 转换为 CLR DateTime

Just use the milliseconds as-is. This makes the JavaScript easier to code also.

只需按原样使用毫秒。这也使 JavaScript 更容易编码。

DateTime ToClrDateTime(Int64 unixEpochMilliseconds)
{
    var clrDateTime = UnixEpochBaseDate + 
        new TimeSpan(unixEpochMilliseconds * TimeSpan.TicksPerMillisecond);
    return clrDateTime;
} 

In JavaScript

在 JavaScript 中

var date = new Date(1381367291665);

回答by Daniel A. White

Its likely that .NET doesn't know what timezone it is. You will have to define this.

.NET 可能不知道它是什么时区。您必须对此进行定义。