简体   繁体   English

如何在Go中格式化此字符串

[英]How to format this string in Go

Here is my program 这是我的程序

package main

import "fmt"
import "time"
import "strconv"
import "strings"

func main() {

    t := time.Date(2016, 10, 30, 14, 0, 0, 0, time.UTC) 


    year, month, day := t.Date()          
    hr := t.Hour()  

    s := []string{strconv.Itoa(year), strconv.Itoa(int(month)), strconv.Itoa(day)}
    date := strings.Join(s, "")

    s = []string{date, strconv.Itoa(hr)}
    date = strings.Join(s, "_")
    fmt.Println(date)       

}

Go Playground 去操场

The output is 输出是

20161030_14

If I replace 如果我更换

t := time.Date(2016, 10, 30, 14, 0, 0, 0, time.UTC)

with

t := time.Date(2016, 6, 3, 9, 0, 0, 0, time.UTC)

then the output is 那么输出是

201663_9

but I hope it outputs 但我希望它能输出

20160603_09

ie the month, day and hour should all be two-character long with possibly adding 0 in front. 也就是说,月份,日期和小时都应为两个字符,并可能在前面加上0 How can I do that? 我怎样才能做到这一点?

Or if you were me, what would be your program which implements the same thing? 或者,如果您是我,那么实现相同功能的程序是什么?

Thanks. 谢谢。

Use the formatting provided by the time package : 使用时间包提供的格式:

https://play.golang.org/p/qIZ58CJqZa https://play.golang.org/p/qIZ58CJqZa

t := time.Date(2016, 6, 3, 9, 0, 0, 0, time.UTC)
fmt.Println(t.Format("20060102_15"))

From the time package documentation on the reference time format string: 从时间包文档中参考时间格式字符串:

The reference time used in the layouts is the specific time: 布局中使用的参考时间是特定时间:

 Mon Jan 2 15:04:05 MST 2006 

which is Unix time 1136239445. Since MST is GMT-0700, the reference time can be thought of as 这是Unix时间1136239445。由于MST是GMT-0700,因此参考时间可以认为是

 01/02 03:04:05PM '06 -0700 

To define your own format, write down what the reference time would look like formatted your way; 要定义自己的格式,请写下参考时间,以哪种方式格式化; see the values of constants like ANSIC, StampMicro or Kitchen for examples. 有关示例,请参见ANSIC,StampMicro或Kitchen等常数的值。 The model is to demonstrate what the reference time looks like so that the Format and Parse methods can apply the same transformation to a general time value. 该模型将演示参考时间的外观,以便Format和Parse方法可以将相同的转换应用于一般时间值。

Within the format string, an underscore _ represents a space that may be replaced by a digit if the following number (a day) has two digits; 在格式字符串中,下划线_表示如果后面的数字(一天)有两位数字,可以用一位数字代替空格。 for compatibility with fixed-width Unix time formats. 与固定宽度的Unix时间格式兼容。

A decimal point followed by one or more zeros represents a fractional second, printed to the given number of decimal places. 小数点后跟一个或多个零表示小数秒,打印到给定的小数位数。 A decimal point followed by one or more nines represents a fractional second, printed to the given number of decimal places, with trailing zeros removed. 小数点后跟一个或多个9代表一个小数秒,打印到给定的小数位数,并删除了尾随的零。 When parsing (only), the input may contain a fractional second field immediately after the seconds field, even if the layout does not signify its presence. 解析时(仅),即使布局不表示其存在,输入也可能在秒字段之后立即包含分数秒字段。 In that case a decimal point followed by a maximal series of digits is parsed as a fractional second. 在这种情况下,小数点后跟最大的数字序列被解析为小数秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM