Skip to content

Instantly share code, notes, and snippets.

@vodolaz095
Created February 19, 2016 15:33
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vodolaz095/a92aa2fee6ada97c31d9 to your computer and use it in GitHub Desktop.
Save vodolaz095/a92aa2fee6ada97c31d9 to your computer and use it in GitHub Desktop.
Get the number of the week in Golang
package main
import(
"fmt"
"time"
)
func NumberOfTheWeekInMonth(now time.Time) int {
beginningOfTheMonth := time.Date(now.Year(), now.Month(), 1, 1, 1, 1, 1, time.UTC)
_, thisWeek := now.ISOWeek()
_, beginningWeek := beginningOfTheMonth.ISOWeek()
return 1 + thisWeek - beginningWeek
}
func main(){
fmt.Printf("This is %d, the %d week in the month", time.Now().Day(),NumberOfTheWeekInMonth(time.Now()))
}
@ashutosh-valuelabs
Copy link

for date 2020-03-31 the NumberOfTheWeekInMonth func is showing 6, i think 6th week is not possible of a month, what should be done in this case.

@vodolaz095
Copy link
Author

yes, it shows number of week from beginning of year in this month

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