Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将ticker值转换为datetime?_C#_Datetime_Ticker - Fatal编程技术网

C# 如何将ticker值转换为datetime?

C# 如何将ticker值转换为datetime?,c#,datetime,ticker,C#,Datetime,Ticker,我有以下价值观: 1465509600000 1402437600000 我尝试了以下方法: 尝试1: public long? CommitmentStartDate { get; set; } public long? CommitmentEndDate { get; set; } public DateTime? CommitmentStartDateDate => new DateTime( (CommitmentStartDate != null ?

我有以下价值观:

1465509600000

1402437600000

我尝试了以下方法:

尝试1:

    public long? CommitmentStartDate { get; set; }

    public long? CommitmentEndDate { get; set; }

    public DateTime? CommitmentStartDateDate => new DateTime( (CommitmentStartDate != null ? (long)CommitmentStartDate: Convert.ToInt64(DateTime.MinValue)) );

    public DateTime? CommitmentEndDateDate => new DateTime(CommitmentEndDate != null ? (long)CommitmentEndDate: Convert.ToInt64(DateTime.MinValue));
这给了我worng格式的日期,我得到:

0001-01-02 16:42:30

0001-01-02 14:57:23

尝试2:

    static readonly DateTime _unixEpoch =
         new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

    public static DateTime DateFromTimestamp(long timestamp)
    {
        return _unixEpoch.AddSeconds(timestamp);
    }

    public long? CommitmentStartDate { get; set; }

    public long? CommitmentEndDate { get; set; }

    public DateTime? CommitmentStartDateDate =>  DateFromTimestamp(CommitmentStartDate != null ? (long)CommitmentStartDate: Convert.ToInt64(DateTime.MinValue));

    public DateTime? CommitmentEndDateDate => DateFromTimestamp(CommitmentEndDate != null ? (long)CommitmentEndDate: Convert.ToInt64(DateTime.MinValue));
这给了我一个argumentOutOfRange例外

我该怎么做

编辑:

预期值:

2014-06-11

2016-06-10

编辑2:

来自日期的蜱虫

1402437600000--->2014-06-11

1465509600000--->2016-06-10


在本部分中,您将添加秒数:

public static DateTime DateFromTimestamp(long timestamp)
{
    return _unixEpoch.AddSeconds(timestamp);
}
如果要传递记号,则需要添加记号:

public static DateTime DateFromTimestamp(long timestamp)
{
    return _unixEpoch.AddTicks(timestamp);
}
但是您必须确保您的滴答声与.NET滴答声的含义相同。一个.NET滴答声为100纳秒。如果您的刻度是不同的单位,则必须首先将其转换为.NET刻度。

我使用了此扩展名

public static DateTime FromUnixTicks(this double ms)
{
    DateTime d1 = new DateTime(1970, 1, 1);
    return d1.AddMilliseconds(ms).ToLocalTime();
}
和样本:

1465509600000.0.FromUnixTicks()
并转换回

public static double ToUnixTicks(this DateTime dt)
{
    DateTime d1 = new DateTime(1970, 1, 1);
    DateTime d2 = dt.ToUniversalTime();
    TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
    return ts.TotalMilliseconds;
}

我需要在这里使用
double
,但我想您可以使用
long

您的两个样本相隔2年,从这些刻度中减去差值,再除以2、365、24和3600,就剩下1000毫秒

快速检查表明,它们确实基于1970年1月1日,所以

//return _unixEpoch.AddSeconds(timestamp);
return _unixEpoch.AddMilliSeconds(timestamp);

可能重复的方式太多绒毛。请只发布一个或多个带有预期日期值的样本。@François Wahl这是我在尝试转换此值时使用的线程,如果您在投票关闭
DateTime
s之前用代码通读整个问题会很好。没有与之相关联的特定格式。您能指定“刻度”是什么吗表示?不管怎样,通过样本“ticks”OP显示,结果将是
2016-06-09 22:00:00
2014-06-10 22:00:00
是的,似乎涉及时区偏移,我得到的基数是1-1-70,02:00。很容易补偿,但对传入数据进行规范总是更好的。