until.js 782 B

123456789101112131415161718192021222324252627
  1. function formatDate(date, fmt) {
  2. if (typeof date == 'string') {
  3. return date;
  4. }
  5. if (!fmt) fmt = "yyyy.MM.dd hh:mm:ss";
  6. if (!date || date == null) return null;
  7. var o = {
  8. 'M+': date.getMonth() + 1, // 月份
  9. 'd+': date.getDate(), // 日
  10. 'h+': date.getHours(), // 小时
  11. 'm+': date.getMinutes(), // 分
  12. 's+': date.getSeconds(), // 秒
  13. 'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
  14. 'S': date.getMilliseconds() // 毫秒
  15. }
  16. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  17. for (var k in o) {
  18. if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : ((
  19. '00' + o[k]).substr(('' + o[k]).length)))
  20. }
  21. return fmt
  22. }
  23. export default {
  24. formatDate
  25. }