球星卡微信小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.9 KiB

  1. /**
  2. * @description 格式化任意时间
  3. * @param {type} date
  4. * @return { fmt }
  5. */
  6. export default function dateFtt(date){
  7. return parseInt(date / 60) + ":" + date % 60
  8. }
  9. /**
  10. * 日期格式化
  11. */
  12. export function dateFormat(date, format) {
  13. format = format || 'yyyy-MM-dd hh:mm:ss';
  14. if (date !== 'Invalid Date') {
  15. let o = {
  16. "M+": date.getMonth() + 1, //month
  17. "d+": date.getDate(), //day
  18. "h+": date.getHours(), //hour
  19. "m+": date.getMinutes(), //minute
  20. "s+": date.getSeconds(), //second
  21. "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
  22. "S": date.getMilliseconds() //millisecond
  23. }
  24. if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
  25. (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  26. for (let k in o)
  27. if (new RegExp("(" + k + ")").test(format))
  28. format = format.replace(RegExp.$1,
  29. RegExp.$1.length === 1 ? o[k] :
  30. ("00" + o[k]).substr(("" + o[k]).length));
  31. return format;
  32. }
  33. return '';
  34. }
  35. /**
  36. * 生成对应的时间字符串
  37. */
  38. export function recordTime(options = {}){
  39. if(Object.prototype.toString.call(options.time) !== '[object Date]'){
  40. options.time = new Date();
  41. }
  42. if(typeof options.separator === 'undefined'){
  43. options.separator = "-";
  44. }
  45. if(typeof options.swf === 'undefined'){
  46. options.swf = "all";
  47. }
  48. let { time, separator, swf } = options;
  49. function cover(par) {
  50. par = par.toString()[1] ? par : "0" + par;
  51. return par;
  52. }
  53. let year = time.getFullYear();
  54. let month = time.getMonth() + 1;
  55. let day = time.getDate();
  56. let hour = time.getHours();
  57. let min = time.getMinutes();
  58. let ppn = time.getSeconds();
  59. if(swf === "time"){
  60. return [hour, min, ppn].map(cover).join(":");
  61. }else if(swf === "date"){
  62. return [year, month, day].map(cover).join(String(separator));
  63. }else{
  64. return [year, month, day].map(cover).join(String(separator)) +" "+ [hour, min, ppn].map(cover).join(":");
  65. }
  66. }