【问题标题】:Understanding the time.Parse function in Go理解 Go 中的 time.Parse 函数
【发布时间】:2017-12-31 00:48:05
【问题描述】:

我目前正在将代码从 go 移植到 c# 并遇到了这段(简化的)代码。 我知道它使用给定格式060102150405 转换给定字符串171228175744.085

官方文档中只有使用2017-Feb-1 等常见格式的示例,而不是这种格式(可能的时间戳?)

我知道这会导致时间蜂鸣2017-12-28 17:57:44.085 +0000 UTC,但我不知道如何,因为我不知道字符串171228175744.085 和布局代表什么。我知道其中一些信息与 GPS 相关。所以,我的问题是:有谁知道如何在 c# 中做到这一点?

package main

import (
    "fmt"
    "time"
)

func main() {
    t, err := time.Parse("060102150405", "171228175744.085")
    if err == nil{
        fmt.Println(t)
    }

}

【问题讨论】:

    标签: c# go time


    【解决方案1】:

    time.Format 周围的文档解释了格式的含义。

    引用:

    Format returns a textual representation of the time value formatted
    according to layout, which defines the format by showing how the 
    reference time, defined to be
    
    Mon Jan 2 15:04:05 -0700 MST 2006
    

    您示例中的格式字符串:060102150405 告诉时间解析器查找以下内容:

    • 06:年
    • 01:月
    • 02:一个月中的哪一天
    • 15:一天中的小时
    • 04:分钟
    • 05:秒

    这是告诉解析器应该如何解释每个数字的便捷方式。如果你仔细看,你会发现数字没有被重复使用,所以当你说 06 时,解析器会将它匹配到 2006

    在 C# 中,您可以使用datetime.ParseExact。大致如下:

    DateTime.ParseExact(dateString, "yyMMddhhmmss", some_provider);
    

    (注意:上面的C#sn-p我没试过,可能需要调整一下)

    【讨论】:

    • 感谢您的快速回复!
    猜你喜欢
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多