System.FormatException:字符串 '638143033743100000' 未被识别为有效的日期时间

问题描述 投票:0回答:2

错误:

System.FormatException: String '638143033743100000' was not recognized as a valid DateTime

当前代码已经规定允许两种不同的日期格式:

items = (await itemService.GetItems())
       .GroupBy(x => x.ItemKey)
       .Select(x => new DisplayItem
       {
           item = x.item,            
           country = x.FirstOrDefault(y => y.item == "Country")?.Value,             
           date = x.FirstOrDefault(y => y.item == "Date")?.Value != null
           ? DateTime.ParseExact(x.FirstOrDefault(y => y.item == "Date")?.Value, 
           new string[] {
            "yyyy-MM-dd HH:mm:ss.FFFFFFF",
            "dd.MM.yyyy HH:mm:ss"}, null, DateTimeStyles.AssumeLocal)
           : (DateTime?)null
       }).Where(s => s.country!= null).OrderByDescending(s => s.date).ToList();       

我们添加了新的日期格式。现在使用滴答时间。我可以知道刻度的格式吗?谢谢。

c# datetime-format
2个回答
2
投票

创建一个新方法并使用它代替 ParseExact 直接调用它。应该使您的代码更具可读性。您在 linq 语句中间处理空案例是一个维护混乱。

.Select(x => new DisplayItem
   {
       item = x.item,            
       country = x.FirstOrDefault(y => y.item == "Country")?.Value,             
       date = MyLovelyDateTimeParse(x.FirstOrDefault(y => y.item == 
       "Date")?.Value),
       ...... etc.
   })



public DateTime? MyLovelyDateTimeParse(string dateString)
{
    if (String.IsNullOrEmpty(dateString))
        return null;
    else if (long.TryParse(dateString, out long value))
        return new DateTime(value);
    else
        return DateTime.ParseExact(dateString,
            new string[] {
            "yyyy-MM-dd HH:mm:ss.FFFFFFF",
            "dd.MM.yyyy HH:mm:ss"}, null, DateTimeStyles.AssumeLocal);          
}

1
投票

编辑:没有看到它是在滴答声中而不是在 UnixTime 中

要从

string
ticks 转换为
DateTime
,将其解析为 long 并使用以下

long ticks = 638143033743100000L;
DateTime dt = new(ticks);

这是一个工作的小提琴

这里是你如何在你的代码中使用它

items = (await itemService.GetItems())
   .GroupBy(x => x.ItemKey)
   .Select(x => new DisplayItem
   {
       item = x.item,            
       country = x.FirstOrDefault(y => y.item == "Country")?.Value,             
       date = (x.FirstOrDefault(y => y.item == "Date") != null) && long.TryParse(x.FirstOrDefault(y => y.item == "Date").Value, out long l) && l > 0
       ? new DateTime(l) : (DateTime?)null
   }).Where(s => s.country!= null).OrderByDescending(s => s.date).ToList();  

然后您可以在您的财产上设置格式

date
稍后下线。


原答案:

这里是如何将时间戳转换为日期时间

首先,将您的字符串转换为长字符串

如果以秒为单位

DateTimeOffset.FromUnixTimeSeconds(long).UtcDateTime

如果以毫秒为单位

DateTimeOffset.FromUnixTimeMilliseconds(l).UtcDateTime;

最好做一个扩展方法

public static class NumericExtensions
{
    public static DateTime ConvertFromTimestampSecondsToDateTime(this long l) => DateTimeOffset.FromUnixTimeSeconds(l).UtcDateTime;
    public static DateTime ConvertFromTimestampMillisecondsToDateTime(this long l) => DateTimeOffset.FromUnixTimeMilliseconds(l).UtcDateTime;
}

然后像这样使用它们

long timestamp = 638143033743100000l;
DateTime dt = timestamp.ConvertFromTimestampMillisecondsToDateTime();
© www.soinside.com 2019 - 2024. All rights reserved.