Go: Best way to get time in format of YYYYMMWDDHHMMSS

huangapple go评论112阅读模式
英文:

Go: Best way to get time in format of YYYYMMWDDHHMMSS

问题

我有一个接受时间参数的 PHP API,格式为YYYYMMWDDHHMMSS。这里的W代表星期几(星期日=0,星期一=1...)。我想按照以下方式生成时间:

  1. package main
  2. import "fmt"
  3. import "time"
  4. func main() {
  5. fmt.Println("Hello, playground")
  6. t := time.Now()
  7. time := t.Format("20060102030405")
  8. fmt.Println(time)
  9. }

但是生成的时间中没有包含星期几,并且我找不到任何格式可以从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:

  1. package main
  2. import "fmt"
  3. import "time"
  4. func main() {
  5. fmt.Println("Hello, playground")
  6. t := time.Now()
  7. time := t.Format("20060102030405")
  8. fmt.Println(time)
  9. }

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

  1. package main
  2. import "fmt"
  3. import "strconv"
  4. import "time"
  5. func main() {
  6. fmt.Println("Hello, playground")
  7. t := time.Now()
  8. time := t.Format("20060102030405")
  9. time = time[:6] + strconv.Itoa(int(t.Weekday())) + time[6:]
  10. fmt.Println(time)
  11. }

Go playground 上试一试。

英文:
  1. package main
  2. import "fmt"
  3. import "strconv"
  4. import "time"
  5. func main() {
  6. fmt.Println("Hello, playground")
  7. t := time.Now()
  8. time := t.Format("20060102030405")
  9. time = time[:6] + strconv.Itoa(int(t.Weekday())) + time[6:]
  10. fmt.Println(time)
  11. }

Try it on the <kbd>Go playground</kbd>

huangapple
  • 本文由 发表于 2015年8月18日 16:09:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/32066978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定