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)

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

报告相同问题?

悬赏问题

  • ¥18 模拟电路问题解答有偿
  • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题