paulguy
paulguy

Reputation: 1115

Get 12 hour from time in Go

Go noob here, and all I want to do is use the time format constants list https://golang.org/src/time/format.go that are mentioned in 3 posts here on SO (https://stackoverflow.com/a/20234207 https://stackoverflow.com/a/14106561 https://stackoverflow.com/a/20234207). None of which including the docs (at least that I can tell) have an example of how to use them.

I would expect this to work (but it clearly does not):

t := time.Now()

log.Println(t.stdHour12()) 

Can you please tell me how to get only the hour (in 12 hour time) for a given time t (ex: 10 from 2021-03-09 22:45:04.009063861 -0500 EST)?

const (
    stdLongMonth      = "January"
    stdMonth          = "Jan"
    stdNumMonth       = "1"
    stdZeroMonth      = "01"
    stdLongWeekDay    = "Monday"
    stdWeekDay        = "Mon"
    stdDay            = "2"
    stdUnderDay       = "_2"
    stdZeroDay        = "02"
    stdHour           = "15"
    stdHour12         = "3"
    stdZeroHour12     = "03"
    stdMinute         = "4"
    stdZeroMinute     = "04"
    stdSecond         = "5"
    stdZeroSecond     = "05"
    stdLongYear       = "2006"
    stdYear           = "06"
    stdPM             = "PM"
    stdpm             = "pm"
    stdTZ             = "MST"
    stdISO8601TZ      = "Z0700"  // prints Z for UTC
    stdISO8601ColonTZ = "Z07:00" // prints Z for UTC
    stdNumTZ          = "-0700"  // always numeric
    stdNumShortTZ     = "-07"    // always numeric
    stdNumColonTZ     = "-07:00" // always numeric
)

Thanks in advance!

EDIT: From the answers received so far, I see that I cannot use the constants above to achieve what I want so I changed the wording of this question to specifically ask to return the hour (and just the hour) for a given time.

Upvotes: 1

Views: 2908

Answers (2)

blami
blami

Reputation: 7431

These are constants representing tokens used internally by formatting code in time package (note they start with lower letter so they aren't exported and you can't even use them outside time package).

If you want to come up with your own format in Go (for both parsing and output) you simply define it using these tokens "as example" and Format() will parse it and apply that format (if valid) to itself.

const (
    MyLayout = "3"
)

func main() {
    t := time.Now()
    fmt.Println(t.Format(MyLayout))
}

Available tokens are listed for example here.

Upvotes: 1

Eli Bendersky
Eli Bendersky

Reputation: 273806

The Time object specifies the full date and time. You can extract just the time, if you like:

func main() {
    t := time.Now()
    fmt.Println(t.Format(time.Kitchen))
}

time.Kitchen is defined in the time package as Kitchen = "3:04PM"

If you want to understand how the format is interpreted, read this piece of documentation carefully


If you just need the hour, call the Hour() method on a Time object. If you want it in 12-hour format, you can just do modulo 12:

func main() {
    t := time.Now()
    fmt.Println(t.Hour())
    fmt.Println(t.Hour() % 12)
}

Upvotes: 4

Related Questions