How to get a day of week from civil.Date - go

How can I get a day of week from civil.Date using the type civil.Date such as Monday, Sunday.
date := civil.Date{
Year: 2021,
Month: time.May,
Day: 1}
I am looking for the equivalent to Weekday() function of 'time' package.
Any alternative way is also welcomed.

How about
package main
import (
"fmt"
"os"
"time"
"cloud.google.com/go/civil"
)
func main() {
date := civil.Date{
Year: 2021,
Month: time.September,
Day: 5,
}
t, err := time.Parse(time.RFC3339, date.String()+"T00:00:00Z")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(t.Weekday().String())
}

Related

Any easier ways to get the current datestamp

I have this code here but I don't think it's elegant. In fact I think it's kind of messy. Does anyone have a better/cleaner/concise code than this? I just need the timestamp of the day.
package main
import (
"os"
"fmt"
"io"
"time"
"strconv"
)
const (
layoutISO = "2006-01-02"
layoutUS = "January 2, 2006"
)
func main() {
year, month, day := time.Now().Date()
dayStr := strconv.Itoa(day)
if len(dayStr) == 1 {
dayStr = "0"+dayStr
}
mthStr := strconv.Itoa(int(month))
if len(mthStr) == 1 {
mthStr = "0"+mthStr
}
layout := strconv.Itoa(year)+"-"+mthStr+"-"+dayStr
fmt.Printf("%v\n",layout)
t, err := time.Parse(layoutISO, layout)
if err != nil {
fmt.Println(err)
}
fmt.Println(t.Unix())
}
This is the answer #Marc suggested. Thanks.
fmt.Printf("Value = %v\n",time.Date(year, month, day, 0,0,0,0, time.UTC).Unix())
I don't fully understand what you're asking, but here are some ways to get the current date and time information.
fmt.Println("current date and time:", time.Now().Format("2 January 2006 15:04:05"))
fmt.Println("current time:", time.Now().Format("15:04:05"))
fmt.Println("current date:", time.Now().Format("2 January 2006"))
https://play.golang.org/p/U2PKjivNzXa

How to update hour, min, sec in golang time?

Ex: How can I update hour in t time?
fmt.Println(t)
//=> 2006-01-02 15:04:05 +0000 UTC
Expect to get: 2006-01-02 00:00:00 +0000 UTC
Edited: similar to: time.Time Round to Day
Use:
t1 := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, t.Nanosecond(), t.Location())
Ref: https://golang.org/pkg/time/#Date
This seems to do it:
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now().UTC()
t = t.Truncate(24 * time.Hour)
fmt.Println(t)
}
https://golang.org/pkg/time#Time.Truncate

time format converted to strftime default format

In the wikipedia entry for Common Log Format, the strftime format is given as:
[10/Oct/2000:13:55:36 -0700] is the date, time, and time zone that the
request was received, by default in strftime format %d/%b/%Y:%H:%M:%S
%z.
When I try using the time.Format function:
package main
import (
"fmt"
"time"
)
func main() {
t, _ := time.Parse(time.UnixDate, "Tue Oct 10 13:55:36 PDT 2000")
fmt.Println(time.Time(t).Format("01/Feb/2006:15:04:05 -0700"))
}
I get the output [10/Feb/2000:13:55:36 +0000], while I was expecting [10/Oct/2000:13:55:36 -0700] (per Wikipedia). What is wrong with my formatting?
I checked that day was a Tuesday and the time zone was -7h (PDT) for that date.
For the Format layout, Jan not Feb. For example,
package main
import (
"fmt"
"time"
)
func main() {
t, err := time.Parse(time.UnixDate, "Tue Oct 10 13:55:36 PDT 2000")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(time.Time(t).Format("01/Jan/2006:15:04:05 -0700"))
}
Output:
10/Oct/2000:13:55:36 +0000
Also, for the time zone, use ParseInLocation,
package main
import (
"fmt"
"time"
)
func main() {
loc, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
fmt.Println(err)
return
}
t, err := time.ParseInLocation(time.UnixDate, "Tue Oct 10 13:55:36 PDT 2000", loc)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(t)
fmt.Println(time.Time(t).Format("01/Jan/2006:15:04:05 -0700"))
}
Playground: https://play.golang.org/p/tBLw5oJSE5t
Output:
2000-10-10 13:55:36 -0700 PDT
10/Oct/2000:13:55:36 -0700

How i can to convert RFC3339 to UNIX in golang

I want to show some RFC3339 time as seconds. I found how to parse times string, but it not that
t, _ := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00")
For example,
package main
import (
"fmt"
"time"
)
func main() {
t, err := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(t)
// Unix returns t as a Unix time,
// the number of seconds elapsed since January 1, 1970 UTC.
fmt.Println(t.Unix())
}
Playground: https://play.golang.org/p/LG6G4lMIWt
Output:
2012-11-01 22:08:41 +0000 UTC
1351807721

How to get hours difference between two dates

I'm working for first time on Go, in this case i have a string on UTC format, I would like to know, how can I get the difference in hours between my date and the time now.
This is my current string
v := "2014-05-03 20:57 UTC"
Use time.Parse and time.Since:
package main
import (
"fmt"
"time"
)
const (
// See http://golang.org/pkg/time/#Parse
timeFormat = "2006-01-02 15:04 MST"
)
func main() {
v := "2014-05-03 20:57 UTC"
then, err := time.Parse(timeFormat, v)
if err != nil {
fmt.Println(err)
return
}
duration := time.Since(then)
fmt.Println(duration.Hours())
}
Have a look at the time package.
package main
import "fmt"
import "time"
func main() {
a, err := time.Parse("2006-01-02 15:04 MST", "2014-05-03 20:57 UTC")
if err != nil {
// ...
return
}
delta := time.Now().Sub(a)
fmt.Println(delta.Hours())
}

Resources