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.

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

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程