英文:
Go: Best way to get time in format of YYYYMMWDDHHMMSS
问题
我有一个接受时间参数的 PHP API,格式为YYYYMMWDDHHMMSS。这里的W代表星期几(星期日=0,星期一=1...)。我想按照以下方式生成时间:
package main
import "fmt"
import "time"
func main() {
fmt.Println("Hello, playground")
t := time.Now()
time := t.Format("20060102030405")
fmt.Println(time)
}
但是生成的时间中没有包含星期几,并且我找不到任何格式可以从time.Format中获取星期几。是否有办法从time.Format()或其他Go API中获取所需的结果呢?
英文:
I have a PHP API that takes time in format of YYYYMMWDDHHMMSS. Here W is weekday(Sunday=0, Monday = 1 ...). I am trying to generate the it like following:
package main
import "fmt"
import "time"
func main() {
fmt.Println("Hello, playground")
t := time.Now()
time := t.Format("20060102030405")
fmt.Println(time)
}
http://play.golang.org/p/Tdamoxi3bE
But it does not have weekday in it and i couldn't find any format to get from time.Format.
Is there any way to get the desired result from time.Format() or any other go
api.
答案1
得分: 4
package main
import "fmt"
import "strconv"
import "time"
func main() {
fmt.Println("Hello, playground")
t := time.Now()
time := t.Format("20060102030405")
time = time[:6] + strconv.Itoa(int(t.Weekday())) + time[6:]
fmt.Println(time)
}
在 Go playground 上试一试。
英文:
package main
import "fmt"
import "strconv"
import "time"
func main() {
fmt.Println("Hello, playground")
t := time.Now()
time := t.Format("20060102030405")
time = time[:6] + strconv.Itoa(int(t.Weekday())) + time[6:]
fmt.Println(time)
}
Try it on the <kbd>Go playground</kbd>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论