Skip to content

Latest commit

 

History

History
57 lines (39 loc) · 1.06 KB

README.md

File metadata and controls

57 lines (39 loc) · 1.06 KB

gotime

  • 为内置的time.Time 输出json时格式化
  • 更加友好的time formatter

Usage

  1. time.Time 时间格式化
func main() {
    // for time.Time 
    t1 := time.Date(2020, 02, 25, 10, 12, 9, 0, time.Local)

    // output: 2020-02-25
    fmt.Println(gotime.FormatDatetime(t1, "YYYY-MM-DD"))
    
    // output: 2020-02-25 10:12:09
    fmt.Println(gotime.FormatDatetime(t1, "YYYY-MM-DD HH:mm:ss"))
}
  1. gotime.Time 时间格式化
func main() {
    // for gotime.Time 
    t1 := gotime.Time(time.Date(2020, 02, 25, 10, 12, 9, 0, time.Local))

    // output: 2020-02-25
    fmt.Println(t1.FormatX( "YYYY-MM-DD"))
    
    // output: 2020-02-25 10:12:09
    fmt.Println(t1.FormatX( "YYYY-MM-DD HH:mm:ss"))
}
  1. json marshal
func main() {
    type aa struct {
        StartTime Time `json:"start_time"`
    }
    
    a := &aa{}
    a.StartTime = Time(time.Date(2020, 02, 25, 10, 12, 9, 0, time.Local))
    data, _ := j.Marshal(a)

    // output: `{"start_time":"2020-02-25 10:12:09"}`
    fmt.Println(string(data))
}