time-helper.js 798 B

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