dongshanjin8947
2017-12-30 16:30
浏览 64

了解Go中的time.Parse函数

I am currently porting code from go to c# and came across this (simplified) piece of code. I do know that it converts a given string 171228175744.085 using a given format 060102150405.

The official docs only have examples using common formats like 2017-Feb-1, not this format (a possible timestamp?)

I do know that this will result in the time beeing 2017-12-28 17:57:44.085 +0000 UTC, but I do not have a clue how, because I have no information what the string 171228175744.085 and layout represent. I do know that some of this information is GPS related. So, my question is: Does anyone know how to do this in c#?

package main

import (
    "fmt"
    "time"
)

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

}

图片转代码服务由CSDN问答提供 功能建议

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

官方文档仅包含使用诸如 2017-Feb-1 ,而不是这种格式(可能是时间戳?)

我确实知道这会导致时间浪费 2017-12-28 17:57:44.085 +0000 UTC ,但是我不知道怎么做,因为我不知道字符串 171228175744.085 和布局代表什么。 我确实知道其中一些信息与GPS有关。 所以,我的问题是:有人知道如何在c#中执行此操作吗?

 包main 
 
import(
“ fmt” 
“ time” 
)  
 
func main(){
t,err:= time.Parse(“ 060102150405”,“ 171228175744.085”)
如果err == nil {
 fmt.Println(t)
} 
 
}  
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doukezi4576 2017-12-30 16:41
    已采纳

    The docs around time.Format explain what the format means.

    Quoting:

    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
    

    The format string in your example: 060102150405 tells the time parser to look for the following:

    • 06: year
    • 01: month
    • 02: day of the month
    • 15: hour of the day
    • 04: minute
    • 05: second

    This is a convenient way of telling the parser how it should interpret each number. If you look carefully, you'll see that numbers are not reused so when you say 06, the parser matches it to 2006.

    In C#, you can use datetime.ParseExact. Something along the lines of:

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

    (note: I have not tried the C# snippet above. You may need to adjust it)

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题