常用函数
t, err := time.Parse(layout,date) // time.Time, error
t := time.Date(1995,time.September,22,13,0,0,0,time.UTC)
formatedTime := t.Format("Mon, 01/02/2006, 15:04") // string
Layout Options
Time | Options |
---|---|
Year | 2006; 06 |
Month | Jan; January; 01; 1 |
Day | 02; 2; _2(For preceding 0) |
Weekday | Mon; Monday |
Hour | 15 ( 24 hour time format ); 3; 03 (AM or PM) |
Minute | 04; 4 |
Second | 05; 5 |
AM/PM Mark | PM |
Day of Year | 002; __2 |
- format constants
const (
Layout = "01/02 03:04:05PM '06 -0700" // The reference time, in numerical order.
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
// Handy time stamps.
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
DateTime = "2006-01-02 15:04:05"
DateOnly = "2006-01-02"
TimeOnly = "15:04:05"
)
Exercise
package booking
import (
"fmt"
"time"
)
// Schedule returns a time.Time from a string containing a date.
func Schedule(date string) time.Time {
layout := "1/2/2006 15:04:05"
t, _ := time.Parse(layout, date)
return t
}
// HasPassed returns whether a date has passed.
func HasPassed(date string) bool {
layout := "January 2, 2006 15:04:05"
t, _ := time.Parse(layout, date)
return time.Now().After(t)
}
// IsAfternoonAppointment returns whether a time is in the afternoon.
func IsAfternoonAppointment(date string) bool {
layout := "Monday, January 2, 2006 15:04:05"
t, _ := time.Parse(layout, date)
if t.Hour() >= 12 && t.Hour() <= 18 {
return true
} else {
return false
}
}
// Description returns a formatted string of the appointment time.
func Description(date string) string {
t := Schedule(date)
return fmt.Sprintf("You have an appointment on %s, %s.",
t.Weekday(),
t.Format("January 2, 2006, at 15:04"))
}
// AnniversaryDate returns a Time with this year's anniversary.
func AnniversaryDate() time.Time {
return time.Date(time.Now().Year(), 9, 15, 0, 0, 0, 0, time.UTC)
}
标签:Jan,15,04,05,Time,基础,Golang,2006,time
From: https://www.cnblogs.com/roadwide/p/17135700.html