time_to.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package mhayaTime
  2. import (
  3. "time"
  4. cstring "github.com/mhaya/extend/string"
  5. )
  6. // ToSecond 输出秒级时间戳
  7. func (c MhayaTime) ToSecond() int64 {
  8. return c.Unix()
  9. }
  10. // ToMillisecond 输出毫秒级时间戳
  11. func (c MhayaTime) ToMillisecond() int64 {
  12. return c.Time.UnixNano() / int64(time.Millisecond)
  13. }
  14. func (c MhayaTime) ToMillisecondString() string {
  15. t := c.ToMillisecond()
  16. return cstring.ToString(t)
  17. }
  18. // ToMicrosecond 输出微秒级时间戳
  19. func (c MhayaTime) ToMicrosecond() int64 {
  20. return c.UnixNano() / int64(time.Microsecond)
  21. }
  22. // ToNanosecond 输出纳秒级时间戳
  23. func (c MhayaTime) ToNanosecond() int64 {
  24. return c.UnixNano()
  25. }
  26. // ToDateMillisecondFormat 2023-04-10 12:26:57.420
  27. func (c MhayaTime) ToDateMillisecondFormat() string {
  28. return c.Format(DateTimeMillisecondFormat)
  29. }
  30. // ToDateTimeFormat 2006-01-02 15:04:05
  31. func (c MhayaTime) ToDateTimeFormat() string {
  32. return c.Format(DateTimeFormat)
  33. }
  34. // ToDateFormat 2006-01-02
  35. func (c MhayaTime) ToDateFormat() string {
  36. return c.Format(DateFormat)
  37. }
  38. // ToTimeFormat 15:04:05
  39. func (c MhayaTime) ToTimeFormat() string {
  40. return c.Format(TimeFormat)
  41. }
  42. // ToShortDateTimeFormat 20060102150405
  43. func (c MhayaTime) ToShortDateTimeFormat() string {
  44. return c.Format(ShortDateTimeFormat)
  45. }
  46. // ToShortDateFormat 20060102
  47. func (c MhayaTime) ToShortDateFormat() string {
  48. return c.Format(ShortDateFormat)
  49. }
  50. // ToShortIntDateFormat 20060102
  51. func (c MhayaTime) ToShortIntDateFormat() int32 {
  52. strDate := c.ToShortDateFormat()
  53. intDate, _ := cstring.ToInt32(strDate)
  54. return intDate
  55. }
  56. // ToShortTimeFormat 150405
  57. func (c MhayaTime) ToShortTimeFormat() string {
  58. return c.Format(ShortTimeFormat)
  59. }
  60. func (c MhayaTime) DailyTOTimeStamp() int64 {
  61. now := Now()
  62. t := time.Date(now.Year(), time.Month(now.Month()), now.Day(), 0, 0, 0, 0, now.Location())
  63. return t.Unix()
  64. }
  65. func (c MhayaTime) TOTimeStamp() int64 {
  66. t := time.Date(c.Year(), time.Month(c.Month()), c.Day(), 0, 0, 0, 0, c.Location())
  67. return t.Unix()
  68. }
  69. func (c MhayaTime) TOHourTimeStamp() int64 {
  70. t := time.Date(c.Year(), time.Month(c.Month()), c.Day(), c.Hour(), 0, 0, 0, c.Location())
  71. return t.Unix()
  72. }