doudui6756 2018-06-10 13:11
浏览 86
已采纳

定义自定义时间格式

I am trying to write a custom date format string as required by my application. Using the Go time package I get the format using a clumsy function (see below).

Also since this function will be called millions of times every day, I want this to be super efficient too. Is there a POSIX style formatting available in Go?

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Now()
    fmt.Printf("Time now is %d%02d%02d%02d%02d%02d",
        t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
}
  • 写回答

1条回答 默认 最新

  • douke7274 2018-06-10 13:13
    关注

    In Go there are built in layouts to pass to the .Format() method of Time (see func (Time) Format), one of these could be what you are looking for:

    func main() {
        fmt.Println(time.Now().Format(time.ANSIC))
        fmt.Println(time.Now().Format(time.UnixDate))
        fmt.Println(time.Now().Format(time.RFC3339))
    }
    

    This is going to be faster than fmt.Printf("...", t.Year(), t.Month(), ...), but we're talking about microseconds "faster" here, so there really is no big difference.

    Output:

    Sun Jun 10 13:18:09 2018
    Sun Jun 10 13:18:09 UTC 2018
    2018-06-10T13:18:09Z
    

    There are a lot more predefined layouts, so you can check all of them out and see if one of them fits your needs. Here they are directly from the source code:

    const (
        ANSIC       = "Mon Jan _2 15:04:05 2006"
        UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
        RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
        RFC822      = "02 Jan 06 15:04 MST"
        RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
        RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
        RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
        RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
        RFC3339     = "2006-01-02T15:04:05Z07:00"
        RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
        Kitchen     = "3:04PM"
        // Handy time stamps.
        Stamp      = "Jan _2 15:04:05"
        StampMilli = "Jan _2 15:04:05.000"
        StampMicro = "Jan _2 15:04:05.000000"
        StampNano  = "Jan _2 15:04:05.000000000"
    )
    

    Other than that, it's just a matter of creating your own layout, there really is no difference.


    To create a custom layout string, quoting from the documentation:

    The layout string used by the Parse function and Format method shows by example how the reference time should be represented. We stress that one must show how the reference time is formatted, not a time of the user's choosing. Thus each layout string is a representation of the time stamp: Jan 2 15:04:05 2006 MST.

    So, for example:

    fmt.Println(time.Now().Format("Mon January 2, 2006 - 15:04:05.000"))
    

    gives:

    Sun June 10, 2018 - 17:49:32.557
    

    If you want something different you'll just have to format the date Jan 2 15:04:05 2006 MST how you would want it to be displayed. You can also take a look at the relative page on Go by Example.

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

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法