dateformat.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const debug = require('debug')('ultrong:dateformat');
  2. module.exports = {
  3. dateFormat: (datestring, category = 'all') => {
  4. if (datestring === undefined || datestring == '')
  5. return '';
  6. const date = new Date(datestring);
  7. const year = date.getFullYear();
  8. const month = (parseInt(date.getMonth()) + 1) < 10 ? '0' + (parseInt(date.getMonth()) + 1).toString() : (parseInt(date.getMonth()) + 1);
  9. const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  10. const hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  11. const minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  12. const seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  13. switch (category) {
  14. case 'all':
  15. return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes;
  16. case 'month-day':
  17. return month + '-' + day;
  18. case 'year-month-day':
  19. return year + '-' + month + '-' + day;
  20. case 'month-day hh:mm:ss':
  21. return month + ' - ' + day + ' ' + hour + ':' + minutes + ':' + seconds;
  22. case 'month-day hh:mm':
  23. return month + ' - ' + day + ' ' + hour + ':' + minutes;
  24. case 'difference':
  25. const newDate = new Date();
  26. if (newDate.getFullYear() > year)
  27. return (newDate.getFullYear() - year) + '年前';
  28. else if (newDate.getMonth() + 1 > month)
  29. return ((newDate.getMonth() + 1) - month) + '月前';
  30. else if (newDate.getDate() > day)
  31. return (newDate.getDate() - day) + '天前';
  32. else if (newDate.getHours() > hour)
  33. return (newDate.getHours() - hour) + '小时前';
  34. else if (newDate.getMinutes() > minutes)
  35. return (newDate.getMinutes() - minutes) + '分钟前';
  36. else if (newDate.getSeconds() > seconds)
  37. return (newDate.getSeconds() - seconds) + '秒前';
  38. else
  39. return '';
  40. default:
  41. return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds;
  42. }
  43. },
  44. getDateString: () => {
  45. let date = new Date();
  46. let year = date.getFullYear();
  47. let month = date.getMonth() < 10 ? '0' + date.getMonth() : date.getMonth();
  48. let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  49. return '_' + year + month + day;
  50. },
  51. //将日期换算成今天的日期,时间不变
  52. getTimeToToday: (date) => {
  53. let cdate = new Date();
  54. const year = cdate.getFullYear();
  55. const month = (parseInt(cdate.getMonth()) + 1) < 10 ? '0' + (parseInt(cdate.getMonth()) + 1).toString() : (parseInt(cdate.getMonth()) + 1);
  56. const day = cdate.getDate() < 10 ? '0' + cdate.getDate() : cdate.getDate();
  57. const hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  58. const minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  59. const seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  60. return new Date(year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds);
  61. }
  62. }