time-helper.js 953 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.stringToDate = stringToDate;
  4. exports.dateToString = dateToString;
  5. var _string = require("../../utils/format/string");
  6. // 字符串转 Date
  7. // 只处理 YYYY-MM-DD 或者 YYYY-MM-DD HH:MM 格式
  8. function stringToDate(timeString) {
  9. if (!timeString) {
  10. return null;
  11. }
  12. return new Date(timeString.replace(/-/g, '/'));
  13. } // Date 转字符串
  14. // type: date or datetime
  15. function dateToString(date, type) {
  16. if (type === void 0) {
  17. type = 'date';
  18. }
  19. if (!date) {
  20. return '';
  21. }
  22. var year = date.getFullYear();
  23. var month = date.getMonth() + 1;
  24. var day = date.getDate();
  25. var timeString = year + "-" + (0, _string.padZero)(month) + "-" + (0, _string.padZero)(day);
  26. if (type === 'datetime') {
  27. var hours = date.getHours();
  28. var minute = date.getMinutes();
  29. timeString += " " + (0, _string.padZero)(hours) + ":" + (0, _string.padZero)(minute);
  30. }
  31. return timeString;
  32. }