time_to.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. }