Go语言 转换时间,时间到字符串

ig9co6j1  于 2023-11-14  发布在  Go
关注(0)|答案(6)|浏览(134)

我试图从我的数据库中添加一些值到Go中的[]string。其中一些是时间戳。
我得到的错误:
无法将U.Created_date(类型time.Time)用作数组元素中的类型字符串
我可以将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)
}
nkoocmlb

nkoocmlb1#

您可以使用Time.String()方法将time.Time转换为string。这将使用格式字符串"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 Playground上的时间总是设置为上面看到的值。在本地运行它以查看当前日期/时间。
还要注意的是,使用Time.Format(),作为布局string,你总是必须传递相同的时间-称为 reference time-以你想要的结果格式化的方式格式化。这在Time.Format()中有记录:
格式返回根据布局设置格式的时间值的文本表示形式,布局通过显示参考时间(定义为

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


如果它是值,则将显示;它作为所需输出的示例。然后将对时间值应用相同的显示规则。

gdrx4gfi

gdrx4gfi2#

package main                                                                                                                                                           

import (
    "fmt"
    "time"
)

// @link https://golang.org/pkg/time/

func main() {

    //caution : format string is `2006-01-02 15:04:05.000000000`
    current := time.Now()

    fmt.Println("origin : ", current.String())
    // origin :  2016-09-02 15:53:07.159994437 +0800 CST

    fmt.Println("mm-dd-yyyy : ", current.Format("01-02-2006"))
    // mm-dd-yyyy :  09-02-2016

    fmt.Println("yyyy-mm-dd : ", current.Format("2006-01-02"))
    // yyyy-mm-dd :  2016-09-02

    // separated by .
    fmt.Println("yyyy.mm.dd : ", current.Format("2006.01.02"))
    // yyyy.mm.dd :  2016.09.02

    fmt.Println("yyyy-mm-dd HH:mm:ss : ", current.Format("2006-01-02 15:04:05"))
    // yyyy-mm-dd HH:mm:ss :  2016-09-02 15:53:07

    // StampMicro
    fmt.Println("yyyy-mm-dd HH:mm:ss: ", current.Format("2006-01-02 15:04:05.000000"))
    // yyyy-mm-dd HH:mm:ss:  2016-09-02 15:53:07.159994

    //StampNano
    fmt.Println("yyyy-mm-dd HH:mm:ss: ", current.Format("2006-01-02 15:04:05.000000000"))
    // yyyy-mm-dd HH:mm:ss:  2016-09-02 15:53:07.159994437
}

字符串

kuuvgm7e

kuuvgm7e3#

package main

import (
    "fmt"
    "time"
)

func main() {
    v , _ := time.Now().UTC().MarshalText()
    fmt.Println(string(v))
}

字符串
输出:2009-11-10T23:00:00Z
Go Playground

fzwojiic

fzwojiic4#

Go Playground http://play.golang.org/p/DN5Py5MxaB

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Now()
    // The Time type implements the Stringer interface -- it
    // has a String() method which gets called automatically by
    // functions like Printf().
    fmt.Printf("%s\n", t)

    // See the Constants section for more formats
    // http://golang.org/pkg/time/#Time.Format
    formatedTime := t.Format(time.RFC1123)
    fmt.Println(formatedTime)
}

字符串

gab6jxml

gab6jxml5#

请在Go Lang中找到修改日期和时间格式的简单解决方案。请在下面找到示例。
软件包链接:https://github.com/vigneshuvi/GoDateFormat
请找到plackholders:https://medium.com/@Martynas/formatting-date-and-time-in-golang-5816112bf098

package main

// Import Package
import (
    "fmt"
    "time"
    "github.com/vigneshuvi/GoDateFormat"
)

func main() {
    fmt.Println("Go Date Format(Today - 'yyyy-MM-dd HH:mm:ss Z'): ", GetToday(GoDateFormat.ConvertFormat("yyyy-MM-dd HH:mm:ss Z")))
    fmt.Println("Go Date Format(Today - 'yyyy-MMM-dd'): ", GetToday(GoDateFormat.ConvertFormat("yyyy-MMM-dd")))
    fmt.Println("Go Time Format(NOW - 'HH:MM:SS'): ", GetToday(GoDateFormat.ConvertFormat("HH:MM:SS")))
    fmt.Println("Go Time Format(NOW - 'HH:MM:SS tt'): ", GetToday(GoDateFormat.ConvertFormat("HH:MM:SS tt")))
}

func GetToday(format string) (todayString string){
    today := time.Now()
    todayString = today.Format(format);
    return
}

字符串

oalqel3c

oalqel3c6#

strconv.Itoa(int(time.Now().Unix()))

字符串

相关问题