util.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const formatDate = (date, fmt = 'yyyy-MM-dd') => {
  2. var o = {
  3. "M+": date.getMonth() + 1, //月份
  4. "d+": date.getDate(), //日
  5. "h+": date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, //小时
  6. "H+": date.getHours(), //小时
  7. "m+": date.getMinutes(), //分
  8. "s+": date.getSeconds(), //秒
  9. "q+": Math.floor((date.getMonth() + 3) / 3), //季度
  10. "S": date.getMilliseconds() //毫秒
  11. };
  12. if (/(y+)/.test(fmt))
  13. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  14. for (var k in o)
  15. if (new RegExp("(" + k + ")").test(fmt))
  16. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  17. return fmt;
  18. }
  19. const formatNumber = n => {
  20. n = n.toString()
  21. return n[1] ? n : '0' + n
  22. }
  23. const checkMobile = mobile => {
  24. return /^1[234578]\d{9}$/.test(mobile)
  25. }
  26. const error = msg => {
  27. wx.showToast({
  28. icon: 'none',
  29. title: msg,
  30. })
  31. }
  32. const success = msg => {
  33. wx.showToast({
  34. title: msg,
  35. })
  36. }
  37. const firstCase = str => {
  38. return str.charAt(0).toUpperCase() + str.slice(1)
  39. }
  40. module.exports = {
  41. formatDate,
  42. checkMobile: checkMobile,
  43. error,
  44. success,
  45. firstCase
  46. }