formatTime.spec.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  5. // +----------------------------------------------------------------------
  6. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  7. // +----------------------------------------------------------------------
  8. // | Author: CRMEB Team <admin@crmeb.com>
  9. // +----------------------------------------------------------------------
  10. import { formatTime } from '@/utils/index.js'
  11. describe('Utils:formatTime', () => {
  12. const d = new Date('2018-07-13 17:54:01') // "2018-07-13 17:54:01"
  13. const retrofit = 5 * 1000
  14. it('ten digits timestamp', () => {
  15. expect(formatTime((d / 1000).toFixed(0))).toBe('7月13日17时54分')
  16. })
  17. it('test now', () => {
  18. expect(formatTime(+new Date() - 1)).toBe('刚刚')
  19. })
  20. it('less two minute', () => {
  21. expect(formatTime(+new Date() - 60 * 2 * 1000 + retrofit)).toBe('2分钟前')
  22. })
  23. it('less two hour', () => {
  24. expect(formatTime(+new Date() - 60 * 60 * 2 * 1000 + retrofit)).toBe('2小时前')
  25. })
  26. it('less one day', () => {
  27. expect(formatTime(+new Date() - 60 * 60 * 24 * 1 * 1000)).toBe('1天前')
  28. })
  29. it('more than one day', () => {
  30. expect(formatTime(d)).toBe('7月13日17时54分')
  31. })
  32. it('format', () => {
  33. expect(formatTime(d, '{y}-{m}-{d} {h}:{i}')).toBe('2018-07-13 17:54')
  34. expect(formatTime(d, '{y}-{m}-{d}')).toBe('2018-07-13')
  35. expect(formatTime(d, '{y}/{m}/{d} {h}-{i}')).toBe('2018/07/13 17-54')
  36. })
  37. })