Guest User

Untitled

a guest
May 26th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. class DateJs {
  2. constructor(date = new Date()) {
  3. if(date === 0) {
  4. this.year =
  5. this.second =
  6. this.millisecond =
  7. this.weekDay =
  8. this.day =
  9. this.hour =
  10. this.month =
  11. this.minute = 0
  12. return
  13. }
  14. this.year = date.getFullYear()
  15. this.second = date.getSeconds()
  16. this.millisecond = date.getMilliseconds()
  17. this.weekDay = date.getDay()
  18. this.day = date.getUTCDate()
  19. this.hour = date.getHours()
  20. this.month = date.getMonth() + 1
  21. this.minute = date.getMinutes()
  22. }
  23. format(str) {
  24. function add0(a) {
  25. return a < 10 ? '0' + a : a
  26. }
  27. let replaces = {
  28. d: this.day,
  29. dd: add0(this.day),
  30. m: this.month,
  31. mm: add0(this.month),
  32. y: this.year,
  33. yy: this.year.toString().substr(2),
  34. h: this.hour,
  35. hh: add0(this.hour),
  36. min: this.minute,
  37. minmin: add0(this.minute),
  38. s: this.second,
  39. ss: add0(this.second),
  40. mil: this.millisecond
  41. }
  42. return str.replace(/[a-z]+/g, function(match) {
  43. return (typeof replaces[match] !== 'undefined' && replaces[match].toString()) || match
  44. })
  45. }
  46. getDate() {
  47. let {year, month, day, hour, minute, second, millisecond} = this
  48. return new Date(year, month-1, day, hour, minute, second, millisecond)
  49.  
  50. }
  51. getTime() {
  52. return this.getDate().getTime()
  53. }
  54. }
  55.  
  56. let date = new DateJs()
  57. date.year = 2019
  58. date.month = 1
  59. date.day = 1
  60.  
  61. console.log(date.format('dd/mm/y, hh:ss:mil'))
Add Comment
Please, Sign In to add comment