func (this *TimeFrame) parseThisNUnits(n int, units string) bool { n = n - 1 switch units { case "minutes": this.Start = now.BeginningOfMinute().Add(time.Duration(-n) * time.Minute) this.End = now.EndOfMinute() case "hours": this.Start = now.BeginningOfHour().Add(time.Duration(-n) * time.Hour) this.End = now.EndOfHour() case "days": this.Start = now.BeginningOfDay().AddDate(0, 0, -n) this.End = now.EndOfDay() case "weeks": this.Start = now.BeginningOfWeek().AddDate(0, 0, -n*7) this.End = now.EndOfWeek() case "months": this.Start = now.BeginningOfMonth().AddDate(0, -n, 0) this.End = now.EndOfMonth() case "years": this.Start = now.BeginningOfYear().AddDate(-n, 0, 0) this.End = now.EndOfYear() default: return false } return true }
func main() { n := time.Now() // 標準ライブラリでフォーマットする fmt.Println(n) // 2016-03-25 19:05:54.785208 +0900 JST fmt.Println(n.Format("2006-01-02")) // 2016-03-25 fmt.Println(n.Format("2006-01-02 15:04:05")) // 2016-03-25 19:05:54 fmt.Println(n.Format(time.ANSIC)) // Fri Mar 25 19:05:54 2016 fmt.Println(n.Format(time.RubyDate)) // Fri Mar 25 19:05:54 +0900 2016 fmt.Println(n.Format(time.RFC3339)) // 2016-03-25T19:05:54+09:00 // timeutil を使ってフォーマットする fmt.Println(timeutil.Strftime(&n, "%Y-%m-%d")) // 2016-03-25 fmt.Println(timeutil.Strftime(&n, "%a %b %d %H:%M:%S %Y")) // Fri Mar 25 19:05:54 2016 // now を使って日付計算をする // 普通に変数名で `now` を使ってると名前解決ができないため、気を付ける必要がありそう fmt.Println(now.BeginningOfMonth()) // 2016-03-01 00:00:00 +0900 JST fmt.Println(now.EndOfMonth()) // 2016-03-31 23:59:59.999999999 +0900 JST }