github.com/sereiner/library@v0.0.0-20200518095232-1fa3e640cc5f/date/date.go (about)

     1  package date
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // TodayTimeStamp 获取今天0点的时间戳
     8  func TodayTimeStamp() int64 {
     9  	n := time.Now()
    10  	return time.Date(n.Year(), n.Month(), n.Day(), 0, 0, 0, 0, time.Local).Unix()
    11  
    12  }
    13  
    14  // TodayTimeStamp 获取今天24点的时间戳
    15  func TodayEndTimeStamp() int64 {
    16  	n := time.Now()
    17  	return time.Date(n.Year(), n.Month(), n.Day(), 23, 59, 59, 999, time.Local).Unix()
    18  
    19  }
    20  
    21  // YesterdayTimeStamp  获取昨天0点的时间戳
    22  func YesterdayTimeStamp() int64 {
    23  	return TodayTimeStamp() - 86400
    24  }
    25  
    26  // YesterdayTimeStamp  获取昨天24点的时间戳
    27  func YesterdayEndTimeStamp() int64 {
    28  	return TodayEndTimeStamp() - 86400
    29  }
    30  
    31  // TodayFormat 获取今天0点的日期
    32  func TodayFormat() string {
    33  	n := time.Now()
    34  	return time.Date(n.Year(), n.Month(), n.Day(), 0, 0, 0, 0, time.Local).Format("2006-01-02 15:04:05")
    35  }
    36  
    37  // TodayEndFormat 获取今天24点的日期
    38  func TodayEndFormat() string {
    39  	n := time.Now()
    40  	return time.Date(n.Year(), n.Month(), n.Day(), 23, 59, 59, 999, time.Local).Format("2006-01-02 15:04:05")
    41  }
    42  
    43  // NowFormat 获取现在的日期
    44  func NowFormat() string {
    45  	return time.Now().Format("2006-01-02 15:04:05")
    46  }
    47  
    48  // TimeStampToDate 时间戳转换为日期格式
    49  func TimeStampToDate(timeStamp int64) string {
    50  	tm := time.Unix(timeStamp, 0)
    51  	return tm.Format("2006-01-02 15:04:05")
    52  }
    53  
    54  func DateToTimeStamp(date string) int64 {
    55  	return str2Time(date).Unix()
    56  }
    57  
    58  /**字符串->时间对象*/
    59  func str2Time(formatTimeStr string) time.Time {
    60  	timeLayout := "2006-01-02 15:04:05"
    61  	loc, _ := time.LoadLocation("Local")
    62  	theTime, _ := time.ParseInLocation(timeLayout, formatTimeStr, loc)
    63  
    64  	return theTime
    65  
    66  }
    67  
    68  /**字符串->时间戳*/
    69  func Str2Stamp(formatTimeStr string) int64 {
    70  	timeStruct := str2Time(formatTimeStr)
    71  	millisecond := timeStruct.UnixNano() / 1e6
    72  	return millisecond
    73  }
    74  
    75  /*时间戳->字符串*/
    76  func stamp2Str(stamp int64) string {
    77  	timeLayout := "2006-01-02 15:04:05"
    78  	str := time.Unix(stamp/1000, 0).Format(timeLayout)
    79  	return str
    80  }
    81  
    82  /*时间戳->时间对象*/
    83  func stamp2Time(stamp int64) time.Time {
    84  	stampStr := stamp2Str(stamp)
    85  	timer := str2Time(stampStr)
    86  	return timer
    87  }