util.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. Date.prototype.format = function (formatStr) {
  2. var str = formatStr
  3. var Week = ['日', '一', '二', '三', '四', '五', '六']
  4. str = str.replace(/yyyy|YYYY/, this.getFullYear())
  5. str = str.replace(
  6. /yy|YY/,
  7. this.getYear() % 100 > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100)
  8. )
  9. str = str.replace(/MM/, this.getMonth() > 8 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1))
  10. str = str.replace(/M/g, this.getMonth() + 1)
  11. str = str.replace(/w|W/g, Week[this.getDay()])
  12. str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate())
  13. str = str.replace(/d|D/g, this.getDate())
  14. str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours())
  15. str = str.replace(/h|H/g, this.getHours())
  16. str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes())
  17. str = str.replace(/m/g, this.getMinutes())
  18. str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds())
  19. str = str.replace(/s|S/g, this.getSeconds())
  20. return str
  21. }
  22. function isMobilePhone(n) {
  23. var reg = /^1[3|4|5|6|7|8|9][0-9]{9}$/
  24. return reg.test(n)
  25. }
  26. function isTelPhone(n) {
  27. var reg = /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/
  28. return reg.test(n)
  29. }
  30. function getDateFromNow(days) {
  31. if (!days) {
  32. return new Date().format('yyyy-MM-dd')
  33. }
  34. var now = new Date().getTime()
  35. var diff = 24 * 3600 * 1000 * days
  36. return new Date(now + diff).format('yyyy-MM-dd')
  37. }
  38. function GetRequest() {
  39. var url = location.search //获取url中"?"符后的字串
  40. var theRequest = new Object()
  41. if (url.indexOf('?') != -1) {
  42. var str = url.substr(1)
  43. strs = str.split('&')
  44. for (var i = 0; i < strs.length; i++) {
  45. theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1]
  46. }
  47. }
  48. return theRequest
  49. }
  50. function getWeek(d) {
  51. var dateArray = d.split('/')
  52. var year = dateArray[0],
  53. month = dateArray[1],
  54. date = dateArray[2]
  55. var dt = new Date(year, month - 1, date)
  56. var weekDay = ['周天', '周一', '周二', '周三', '周四', '周五', '周六']
  57. return weekDay[dt.getDay()]
  58. }