hours.js 551 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. const DatePart = require('./datepart');
  3. class Hours extends DatePart {
  4. constructor(opts = {}) {
  5. super(opts);
  6. }
  7. up() {
  8. this.date.setHours(this.date.getHours() + 1);
  9. }
  10. down() {
  11. this.date.setHours(this.date.getHours() - 1);
  12. }
  13. setTo(val) {
  14. this.date.setHours(parseInt(val.substr(-2)));
  15. }
  16. toString() {
  17. let hours = this.date.getHours();
  18. if (/h/.test(this.token)) hours = hours % 12 || 12;
  19. return this.token.length > 1 ? String(hours).padStart(2, '0') : hours;
  20. }
  21. }
  22. module.exports = Hours;