当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


GO Time.String用法及代码示例

GO语言"time"包中"Time.String"类型的用法及代码示例。

用法:

func(t Time) String() string

String 返回使用格式字符串格式化的时间

"2006-01-02 15:04:05.999999999 -0700 MST"

如果时间具有单调时钟读数,则返回的字符串包括最后一个字段"m=±<value>",其中 value 是格式化为十进制秒数的单调时钟读数。

返回的字符串用于调试;要获得稳定的序列化表示,请使用 t MarshalText t MarshalBinary 或 t.Format 与显式格式字符串。

例子:

package main

import (
    "fmt"
    "time"
)

func main() {
    timeWithNanoseconds := time.Date(2000, 2, 1, 12, 13, 14, 15, time.UTC)
    withNanoseconds := timeWithNanoseconds.String()

    timeWithoutNanoseconds := time.Date(2000, 2, 1, 12, 13, 14, 0, time.UTC)
    withoutNanoseconds := timeWithoutNanoseconds.String()

    fmt.Printf("withNanoseconds = %v\n", string(withNanoseconds))
    fmt.Printf("withoutNanoseconds = %v\n", string(withoutNanoseconds))

}

输出:

withNanoseconds = 2000-02-01 12:13:14.000000015 +0000 UTC
withoutNanoseconds = 2000-02-01 12:13:14 +0000 UTC

相关用法


注:本文由纯净天空筛选整理自golang.google.cn大神的英文原创作品 Time.String。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。