day.js 862 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. const DatePart = require('./datepart');
  3. const pos = n => {
  4. n = n % 10;
  5. return n === 1 ? 'st'
  6. : n === 2 ? 'nd'
  7. : n === 3 ? 'rd'
  8. : 'th';
  9. }
  10. class Day extends DatePart {
  11. constructor(opts={}) {
  12. super(opts);
  13. }
  14. up() {
  15. this.date.setDate(this.date.getDate() + 1);
  16. }
  17. down() {
  18. this.date.setDate(this.date.getDate() - 1);
  19. }
  20. setTo(val) {
  21. this.date.setDate(parseInt(val.substr(-2)));
  22. }
  23. toString() {
  24. let date = this.date.getDate();
  25. let day = this.date.getDay();
  26. return this.token === 'DD' ? String(date).padStart(2, '0')
  27. : this.token === 'Do' ? date + pos(date)
  28. : this.token === 'd' ? day + 1
  29. : this.token === 'ddd' ? this.locales.weekdaysShort[day]
  30. : this.token === 'dddd' ? this.locales.weekdays[day]
  31. : date;
  32. }
  33. }
  34. module.exports = Day;