Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

2 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import date_utils from '../src/date_utils';
  2. test('Parse: parses string date', () => {
  3. const date = date_utils.parse('2017-09-09');
  4. expect(date.getDate()).toBe(9);
  5. expect(date.getMonth()).toBe(8);
  6. expect(date.getFullYear()).toBe(2017);
  7. });
  8. test('Parse: parses string datetime', () => {
  9. const date = date_utils.parse('2017-08-27 16:08:34');
  10. expect(date.getFullYear()).toBe(2017);
  11. expect(date.getMonth()).toBe(7);
  12. expect(date.getDate()).toBe(27);
  13. expect(date.getHours()).toBe(16);
  14. expect(date.getMinutes()).toBe(8);
  15. expect(date.getSeconds()).toBe(34);
  16. });
  17. test('Parse: parses string datetime', () => {
  18. const date = date_utils.parse('2016-02-29 16:08:34.3');
  19. expect(date.getFullYear()).toBe(2016);
  20. expect(date.getMonth()).toBe(1);
  21. expect(date.getDate()).toBe(29);
  22. expect(date.getHours()).toBe(16);
  23. expect(date.getMinutes()).toBe(8);
  24. expect(date.getSeconds()).toBe(34);
  25. expect(date.getMilliseconds()).toBe(300);
  26. });
  27. test('Parse: parses string datetime', () => {
  28. const date = date_utils.parse('2015-07-01 00:00:59.200');
  29. expect(date.getFullYear()).toBe(2015);
  30. expect(date.getMonth()).toBe(6);
  31. expect(date.getDate()).toBe(1);
  32. expect(date.getHours()).toBe(0);
  33. expect(date.getMinutes()).toBe(0);
  34. expect(date.getSeconds()).toBe(59);
  35. expect(date.getMilliseconds()).toBe(200);
  36. });
  37. test('Format: converts date object to string', () => {
  38. const date = new Date('2017-09-18');
  39. expect(date_utils.to_string(date)).toBe('2017-09-18');
  40. });
  41. test('Format: converts date object to string', () => {
  42. const date = new Date('2016-02-29 16:08:34.3');
  43. expect(date_utils.to_string(date, true)).toBe('2016-02-29 16:08:34.300');
  44. });
  45. test('Format: converts date object to string', () => {
  46. const date = new Date('2016-02-29 16:08:34.3');
  47. expect(date_utils.to_string(date, true)).toBe('2016-02-29 16:08:34.300');
  48. });
  49. test('Parse: returns Date Object as is', () => {
  50. const d = new Date();
  51. const date = date_utils.parse(d);
  52. expect(d).toBe(date);
  53. });
  54. test('Diff: returns diff between 2 date objects', () => {
  55. const a = date_utils.parse('2017-09-08');
  56. const b = date_utils.parse('2017-06-07');
  57. expect(date_utils.diff(a, b, 'day')).toBe(93);
  58. expect(date_utils.diff(a, b, 'month')).toBe(3);
  59. expect(date_utils.diff(a, b, 'year')).toBe(0);
  60. });
  61. test('StartOf', () => {
  62. const date = date_utils.parse('2017-08-12 15:07:34.012');
  63. const start_of_millisecond = date_utils.start_of(date, 'millisecond');
  64. expect(date_utils.to_string(start_of_millisecond, true)).toBe(
  65. '2017-08-12 15:07:34.012'
  66. );
  67. const start_of_second = date_utils.start_of(date, 'second');
  68. expect(date_utils.to_string(start_of_second, true)).toBe(
  69. '2017-08-12 15:07:34.000'
  70. );
  71. const start_of_minute = date_utils.start_of(date, 'minute');
  72. expect(date_utils.to_string(start_of_minute, true)).toBe(
  73. '2017-08-12 15:07:00.000'
  74. );
  75. const start_of_hour = date_utils.start_of(date, 'hour');
  76. expect(date_utils.to_string(start_of_hour, true)).toBe(
  77. '2017-08-12 15:00:00.000'
  78. );
  79. const start_of_day = date_utils.start_of(date, 'day');
  80. expect(date_utils.to_string(start_of_day, true)).toBe(
  81. '2017-08-12 00:00:00.000'
  82. );
  83. const start_of_month = date_utils.start_of(date, 'month');
  84. expect(date_utils.to_string(start_of_month, true)).toBe(
  85. '2017-08-01 00:00:00.000'
  86. );
  87. const start_of_year = date_utils.start_of(date, 'year');
  88. expect(date_utils.to_string(start_of_year, true)).toBe(
  89. '2017-01-01 00:00:00.000'
  90. );
  91. });
  92. test('format', () => {
  93. const date = date_utils.parse('2017-08-12 15:07:23');
  94. expect(date_utils.format(date, 'YYYY-MM-DD')).toBe('2017-08-12');
  95. });
  96. test('format', () => {
  97. const date = date_utils.parse('2016-02-29 16:08:34.3');
  98. expect(date_utils.format(date)).toBe('2016-02-29 16:08:34.300');
  99. });