datetime.go 972 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package datetime
  2. import (
  3. "fmt"
  4. "time"
  5. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/i18n"
  6. )
  7. const (
  8. dateTimeFormat = "2006-01-02"
  9. yearFormat = "2006"
  10. monthFormat = "01"
  11. formDayFormat = "02"
  12. dayFormat = "2"
  13. )
  14. func ParseDateTime(dateTime string) (time.Time, error) {
  15. return time.Parse(dateTimeFormat, dateTime)
  16. }
  17. func FormatDateStr(lang i18n.Language, date time.Time) string {
  18. switch lang {
  19. case i18n.En:
  20. return getMonthStr(lang, date.Format(monthFormat)) + " " +
  21. date.Format(dayFormat) + " " +
  22. date.Format(yearFormat)
  23. default:
  24. return date.Format(dayFormat) + " " +
  25. getMonthStr(lang, date.Format(monthFormat)) + " " +
  26. date.Format(yearFormat)
  27. }
  28. }
  29. func FormatDateForm(date time.Time) string {
  30. return date.Format(yearFormat) + "-" + date.Format(monthFormat) + "-" + date.Format(formDayFormat)
  31. }
  32. func getMonthStr(lang i18n.Language, month string) string {
  33. return i18n.T(lang, fmt.Sprintf("m%s", month))
  34. }