Api中未知的日期格式

本文关键字:日期 格式 未知 Api | 更新日期: 2023-09-27 18:05:50

我正在使用HighStock图表链接

它使用来自api的数据

第一个参数是日期,如

[
/* Sep 2009 */
[1252368000000,24.70],
..
]

这个日期格式是什么?如何得到这种格式是c# ?

Api中未知的日期格式

这似乎是一个JavaScript日期值,以1970年1月1日以来经过的毫秒为单位。

在c#中有几种方法可以将其转换为DateTime

例如(从上面的链接),您可以添加自JavaScript纪元以来经过的时间。要将毫秒转换为刻度,请乘以10,000。因此,您可以这样写:

new DateTime(1970, 1, 1).AddTicks(1252368000000 * 10000);

 /// <summary>
    /// Dates  represented as Unix timestamp 
    /// with slight modification: it defined as the number
    /// of seconds that have elapsed since 00:00:00, Thursday, 1 January 1970.
    /// To convert it to .NET DateTime use following extension
    /// </summary>
    /// <param name="_time">DateTime</param>
    /// <returns>Return as DateTime of uint time
    /// </returns>
    public DateTime ToDateTime( uint _time)
    {
        return new DateTime(1970, 1, 1).AddSeconds(_time);
    }
    /// <summary>
    /// Dates  represented as Unix timestamp 
    /// with slight modification: it defined as the number
    /// of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970.
    /// To convert .NET DateTime to Unix time use following extension
    /// </summary>
    /// <param name="_time">DateTime</param>
    /// <returns>
    /// Return as uint time of DateTime
    /// </returns>
    public uint ToUnixTime(DateTime _time)
    {
        return (uint)_time.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
    }