Go: Best way to get time in format of YYYYMMWDDHHMMSS

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

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>

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:

确定