Skip to content

Instantly share code, notes, and snippets.

@linw1995
Created August 27, 2021 02:37
Show Gist options
  • Save linw1995/9b2e7d53dcf4706279341bb3597dda01 to your computer and use it in GitHub Desktop.
Save linw1995/9b2e7d53dcf4706279341bb3597dda01 to your computer and use it in GitHub Desktop.
get begin time and end time in Golang.
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(
now.Truncate(time.Hour*24),
now.Truncate(time.Hour*24).Add(time.Hour*23+time.Minute*59+time.Second*59),
now,
)
}
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(
now.AddDate(0, 0, 1-now.Day()).Truncate(time.Hour*24),
now.AddDate(0, 1, -now.Day()).Truncate(time.Hour*24).Add(time.Hour*23+time.Minute*59+time.Second*59),
now,
)
}
@linw1995
Copy link
Author

linw1995 commented Dec 7, 2021

// You can edit this code!
// Click here and start typing.
package main

import (
	"fmt"
	"time"
)

func MonthDays(firstDayOfMonth time.Time) int {
	diff := firstDayOfMonth.AddDate(0, 1, 0).Sub(firstDayOfMonth)
	return int(diff.Hours()) / 24
}

func main() {
	t, err := time.ParseInLocation("2006-01", "2019-12", time.Local)
	if err != nil {
		panic(err)
	}
	fmt.Println("Hello, 世界", MonthDays(t))
}

@xvbnm48
Copy link

xvbnm48 commented Oct 28, 2022

thanks for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment