1

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.

paulguy
  • 1,045
  • 16
  • 28

2 Answers2

4

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)
}
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • Can you explain this a bit? Why `(time.Kitchen)`? Also, I just tested it in the go playground and it yields `11:00PM` (note that it is 10:39PM). – paulguy Mar 10 '21 at 03:39
  • 1
    @paulguy: added details. Note that the playground is a sandboxed environment and not a "real" machine. Try running the code locally on your computer, you should get the right time. – Eli Bendersky Mar 10 '21 at 03:40
  • Thanks, I tried it on my computer and you are correct. I have edited the question to be more specific to my goal of getting only the hour (in 12 hour time). Sorry for the confusion. – paulguy Mar 10 '21 at 03:48
  • 1
    @paulguy: I've updated my answer based on your edit and question in the comment – Eli Bendersky Mar 10 '21 at 03:53
1

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.

blami
  • 6,588
  • 2
  • 23
  • 31
  • I guess it still doesn't make sense to me how to use this to only return the current hour. From what you've shown (and looking at the constants from the docs) I would "come up with" something like `const (StdHour12 = "3")` but that doesn't work as expected (yields 6 when it is currently 10). Also, your `MyLayout` did not return the correct time (yields Tue 10:11PM when it is currently Tue 10:24PM). – paulguy Mar 10 '21 at 03:24
  • Sorry, I meant to also say that regarding your "Available tokens are listed for example...", I looked at it and I don't see how to use say: `Hours 03 3 15` to get just the hour in 12 hour time. – paulguy Mar 10 '21 at 03:31
  • 1
    Thats probably because you tried it in Playground which is sandbox and reports incorrect time. To get just hour from time you can use `t.Hours() % 12` which doesn't require any parsing and formatting. – blami Mar 10 '21 at 04:22