dongshanjin8947 2017-12-30 16:30
浏览 101
已采纳

了解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)
    }

}
  • 写回答

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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制