Golang:将time.Time转换为字符串 [英] Golang: convert time.Time to string
问题描述
我试图将我的数据库中的一些值添加到Go中的 []字符串
。
我得到错误:
无法使用我可以将
time.Time to
string
?type UsersSession struct {
Userid int
时间戳time.Time
Created_date time.Time
}
类型用户struct {
名称字符串
电子邮件字符串
国家字符串
Created_date time.Time
Id int
哈希字符串
IP字符串
}
-
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 minutes> = now())
U: = Users {}
US:= UsersSe ssion {}
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}
// -------------
// ^这是发生错误的位置
//不能使用U.Created_date(输入时间.Time)作为数组元素的类型字符串(对于US.Created_date和US.Timestamp也是)
// -------------
usersArray = append (usersArray,user)
log.Print(usersArray:,usersArray)
}
编辑
我添加了以下内容。它现在有效,谢谢。
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
更改为字符串
。这使用格式字符串2006-01-02 15:04:05.999999999 -0700 MST
。
如果您需要其他自定义格式,您可以使用
Time.Format()
。例如,要获取yyyy-MM-dd HH:mm:ss
格式的时间戳,请使用格式字符串2006-01-02 15 :04:05
。
示例:
t:= time.Now()
fmt.Println(t.String())
fmt.Println(t.Format(2006-01-02 15:04:05 ))
输出(在 Go Playground ):
2009-11-10 23 :00:00 +0000 UTC
2009-11-10 23:00:00
注意:Go游乐场的时间总是设置为上面的值。
另外请注意,使用
Time.Format()
作为布局string
你总是必须经过同一时间 - 称为 reference time-格式化你希望格式化结果的方式。这记录在Time.Format()
:
$ b
格式返回一个文本表示根据布局格式化的时间值,通过显示参考时间如何定义格式来定义格式。定义为
Mon Jan 2 15:04:05 -0700 MST 2006
会显示,如果它是值;它充当期望输出的一个例子。相同的显示规则将应用于时间值。
I'm trying to add some values from my database to a
[]string
in Go. Some of these are timestamps.I get the error:
cannot use U.Created_date (type time.Time) as type string in array element
Can I convert
time.Time
tostring
?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) }
EDIT
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")
解决方案You can use the
Time.String()
method to convert atime.Time
to astring
. This uses the format string"2006-01-02 15:04:05.999999999 -0700 MST"
.If you need other custom format, you can use
Time.Format()
. For example to get the timestamp in the format ofyyyy-MM-dd HH:mm:ss
use the format string"2006-01-02 15:04:05"
.Example:
t := time.Now() fmt.Println(t.String()) fmt.Println(t.Format("2006-01-02 15:04:05"))
Output (try it on the Go Playground):
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.
Also note that using
Time.Format()
, as the layoutstring
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 atTime.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.
这篇关于Golang:将time.Time转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!