将time.Time转换为字符串 [英] Convert time.Time to string

查看:300
本文介绍了将time.Time转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据库中的某些值添加到Go中的[]string中.其中一些是时间戳.

I'm trying to add some values from my database to a []string in Go. Some of these are timestamps.

我得到了错误:

不能使用U.Created_date(类型time.Time)作为数组元素中的类型字符串

cannot use U.Created_date (type time.Time) as type string in array element

我可以将time.Time转换为string吗?

type UsersSession struct {
    Userid int
    Timestamp time.Time
    Created_date time.Time
}

type Users struct {
    Name string
    Email string
    Country string
    Created_date time.Time
    Id int
    Hash string
    IP string
}

-

var usersArray = [][]string{}

rows, err := db.Query("SELECT u.id, u.hash, u.name, u.email, u.country, u.IP, u.created_date, us.timestamp, us.created_date FROM usersSession AS us LEFT JOIN users AS u ON u.id = us.userid WHERE us.timestamp + interval 30 minute >= now()")

U := Users{}
US := UsersSession{}

for rows.Next() {
    err = rows.Scan(&U.Id, &U.Hash, &U.Name, &U.Email, &U.Country, &U.IP, &U.Created_date, &US.Timestamp, &US.Created_date)
    checkErr(err)

    userid_string := strconv.Itoa(U.Id)
    user := []string{userid_string, U.Hash, U.Name, U.Email, U.Country, U.IP, U.Created_date, US.Timestamp, US.Created_date}
    // -------------
    // ^ this is where the error occurs
    // cannot use U.Created_date (type time.Time) as type string in array element (for US.Created_date and US.Timestamp aswell)
    // -------------

    usersArray = append(usersArray, user)
    log.Print("usersArray: ", usersArray)
}

编辑

我添加了以下内容.现在可以了,谢谢.

I added the following. It works now, thanks.

userCreatedDate := U.Created_date.Format("2006-01-02 15:04:05")
userSessionCreatedDate := US.Created_date.Format("2006-01-02 15:04:05")
userSessionTimestamp := US.Timestamp.Format("2006-01-02 15:04:05")

推荐答案

您可以使用 Time.String() 方法将 time.Time 转换为string.这使用格式字符串"2006-01-02 15:04:05.999999999 -0700 MST".

You can use the Time.String() method to convert a time.Time to a string. This uses the format string "2006-01-02 15:04:05.999999999 -0700 MST".

如果需要其他自定义格式,则可以使用 Time.Format() .例如,要获取yyyy-MM-dd HH:mm:ss格式的时间戳,请使用格式字符串"2006-01-02 15:04:05".

If you need other custom format, you can use Time.Format(). For example to get the timestamp in the format of yyyy-MM-dd HH:mm:ss use the format string "2006-01-02 15:04:05".

示例:

t := time.Now()
fmt.Println(t.String())
fmt.Println(t.Format("2006-01-02 15:04:05"))

输出(在转到操场上尝试):

2009-11-10 23:00:00 +0000 UTC
2009-11-10 23:00:00

注意:围棋操场"上的时间始终设置为上面看到的值.在本地运行以查看当前日期/时间.

Note: time on the Go Playground is always set to the value seen above. Run it locally to see current date/time.

还请注意,使用Time.Format()作为布局string时,您始终必须以希望格式化结果的方式传递相同的时间-称为 reference 时间.记录在Time.Format():

Also note that using Time.Format(), as the layout string you always have to pass the same time –called the reference time– formatted in a way you want the result to be formatted. This is documented at Time.Format():

Format返回根据布局格式化的时间值的文本表示,该时间值通过显示参考时间的定义方式定义了格式

Format returns a textual representation of the time value formatted according to layout, which defines the format by showing how the reference time, defined to be

Mon Jan 2 15:04:05 -0700 MST 2006

如果是

,将显示该值;它作为所需输出的示例.相同的显示规则将应用于时间值.

would be displayed if it were the value; it serves as an example of the desired output. The same display rules will then be applied to the time value.

这篇关于将time.Time转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆