How do I reference the day of the week as an integer?

4,120 views
Skip to first unread message

pootwa...@gmail.com

unread,
Jul 2, 2014, 9:41:19 PM7/2/14
to golan...@googlegroups.com
Currently time.Weekday is declared as follows:
// A Weekday specifies a day of the week (Sunday = 0, ...).
type Weekday int

const (
Sunday Weekday = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
)


And, unfortunately, the time.Weekday() function returns a string (Name of the day) rather than an integer.

Caleb Spare

unread,
Jul 3, 2014, 2:56:59 AM7/3/14
to pootwa...@gmail.com, golang-nuts
No, t.Weekday() returns a time.Weekday, not a string. You can turn it
into an int with an int conversion: int(yourweekday).

You may be thinking it's a string because if how it's being formatted
when you inspect it (because a time.Weekday has a String method
defined on it).
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Sean Gao

unread,
Jul 3, 2014, 3:05:45 AM7/3/14
to pootwa...@gmail.com, golan...@googlegroups.com
Have you tried something like:
    fmt.Printf("%d", time.Weekday(0))

Or:
    int(time.Weekday(5))


Regards,
Sean



--

Bryan Jarvis

unread,
Jul 3, 2014, 2:35:22 PM7/3/14
to golan...@googlegroups.com, pootwa...@gmail.com

Thanks, Caleb!  That was the key, casting it as an integer.

So:
t := time.Now()
    d1 := int(t.Weekday())
returns the integer I am looking for!
Reply all
Reply to author
Forward
0 new messages