码迷,mamicode.com
首页 > 其他好文 > 详细

4.2 时间格式转为字符串格式

时间:2018-03-22 01:47:26      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:str   format   func   ring   字符串   red   printf   mat   one   


package main

import (
    "fmt"
    "time"
)

func main() {
    tTime := time.Date(2017, time.March, 5, 8, 5, 2, 0, time.Local)

    // The formatting is done
    // with use of reference value
    // Jan 2 15:04:05 2006 MST
    fmt.Printf("tTime is: %s\n", tTime.Format("2006/1/2"))

    fmt.Printf("The time is: %s\n", tTime.Format("15:04"))

    //The predefined formats could
    // be used
    fmt.Printf("The time is: %s\n", tTime.Format(time.RFC1123))

    // The formatting supports space padding
    //only for days in Go version 1.9.2
    fmt.Printf("tTime is: %s\n", tTime.Format("2006/1/_2"))

    // The zero padding is done by adding 0
    fmt.Printf("tTime is: %s\n", tTime.Format("2006/01/02"))

    //The fraction with leading zeros use 0s
    fmt.Printf("tTime is: %s\n", tTime.Format("15:04:05.00"))

    //The fraction without leading zeros use 9s
    fmt.Printf("tTime is: %s\n", tTime.Format("15:04:05.999"))

    // Append format appends the formatted time to given
    // buffer
    fmt.Println(string(tTime.AppendFormat([]byte("The time is up: "), "03:04PM")))
}

/*
tTime is: 2017/3/5
The time is: 08:05
The time is: Sun, 05 Mar 2017 08:05:02 CST
tTime is: 2017/3/ 5
tTime is: 2017/03/05
tTime is: 08:05:02.00
tTime is: 08:05:02
The time is up: 08:05AM

*/

4.2 时间格式转为字符串格式

标签:str   format   func   ring   字符串   red   printf   mat   one   

原文地址:https://www.cnblogs.com/zrdpy/p/8620826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!