edb9d8fafdda73c01e38e116c842d0b5d778b8feffdd5ea5ac42ccac21734b14c498fa7a17bf6c302fa4e30e3fdd58570e321a0f788e62028af774b4472149 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { normalizeUnits } from '../units/aliases';
  2. export function startOf (units) {
  3. units = normalizeUnits(units);
  4. // the following switch intentionally omits break keywords
  5. // to utilize falling through the cases.
  6. switch (units) {
  7. case 'year':
  8. this.month(0);
  9. /* falls through */
  10. case 'quarter':
  11. case 'month':
  12. this.date(1);
  13. /* falls through */
  14. case 'week':
  15. case 'isoWeek':
  16. case 'day':
  17. case 'date':
  18. this.hours(0);
  19. /* falls through */
  20. case 'hour':
  21. this.minutes(0);
  22. /* falls through */
  23. case 'minute':
  24. this.seconds(0);
  25. /* falls through */
  26. case 'second':
  27. this.milliseconds(0);
  28. }
  29. // weeks are a special case
  30. if (units === 'week') {
  31. this.weekday(0);
  32. }
  33. if (units === 'isoWeek') {
  34. this.isoWeekday(1);
  35. }
  36. // quarters are also special
  37. if (units === 'quarter') {
  38. this.month(Math.floor(this.month() / 3) * 3);
  39. }
  40. return this;
  41. }
  42. export function endOf (units) {
  43. units = normalizeUnits(units);
  44. if (units === undefined || units === 'millisecond') {
  45. return this;
  46. }
  47. // 'date' is an alias for 'day', so it should be considered as such.
  48. if (units === 'date') {
  49. units = 'day';
  50. }
  51. return this.startOf(units).add(1, (units === 'isoWeek' ? 'week' : units)).subtract(1, 'ms');
  52. }