date.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. graceUI-JS - 时间转换函数工具
  3. link : graceui.hcoder.net
  4. author : 5213606@qq.com 深海
  5. 版权声明 :
  6. GraceUI 的版权约束是不能转售或者将 GraceUI 直接发布到公开渠道!
  7. 侵权必究,请遵守版权约定!
  8. */
  9. module.exports = {
  10. // 获取当前时间
  11. currentTime : function (returnType, addVal) {
  12. var dateObj = new Date();
  13. var cTime = dateObj.getTime();
  14. if(addVal){cTime += addVal;}
  15. if(!returnType){return cTime;}
  16. return this.formatDateTime(cTime / 1000, 'str');
  17. },
  18. // 时间戳转 YY-mm-dd HH:ii:ss
  19. formatDateTime : function(timeStamp, returnType){
  20. timeStamp = parseInt(timeStamp);
  21. var date = new Date();
  22. if(timeStamp < 90000000000 ){
  23. date.setTime(timeStamp * 1000);
  24. }else{
  25. date.setTime(timeStamp );
  26. }
  27. var y = date.getFullYear();
  28. var m = date.getMonth() + 1;
  29. m = m < 10 ? ('0' + m) : m;
  30. var d = date.getDate();
  31. d = d < 10 ? ('0' + d) : d;
  32. var h = date.getHours();
  33. h = h < 10 ? ('0' + h) : h;
  34. var minute = date.getMinutes();
  35. var second = date.getSeconds();
  36. minute = minute < 10 ? ('0' + minute) : minute;
  37. second = second < 10 ? ('0' + second) : second;
  38. if(returnType == 'str'){return y + '-' + m + '-' + d + ' '+ h +':' + minute + ':' + second;}
  39. return [y, m, d, h, minute, second];
  40. },
  41. // 日期转时间戳
  42. dateTimeToTimeStamp : function(timeStamp){
  43. var reg = /^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/;
  44. var res = timeStamp.match(reg);
  45. if (res == null){
  46. var reg2 = /^([0-9]{2})\/([0-9]{2})\/([0-9]{4}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/;
  47. var res2 = timeStamp.match(reg2);
  48. if(res2 == null){
  49. console.log('时间格式错误 E001');
  50. return false;
  51. }else{
  52. var year = parseInt(res2[3]);
  53. var month = parseInt(res2[1]);
  54. var day = parseInt(res2[2]);
  55. var h = parseInt(res2[4]);
  56. var i = parseInt(res2[5]);
  57. var s = parseInt(res2[6]);
  58. }
  59. }else{
  60. var year = parseInt(res[1]);
  61. var month = parseInt(res[2]);
  62. var day = parseInt(res[3]);
  63. var h = parseInt(res[4]);
  64. var i = parseInt(res[5]);
  65. var s = parseInt(res[6]);
  66. }
  67. if (year < 1000) { console.log('时间格式错误'); return false; }
  68. if (h < 0 || h > 24) { console.log('时间格式错误'); return false; }
  69. if (i < 0 || i > 60) { console.log('时间格式错误'); return false; }
  70. if (s < 0 || s > 60) { console.log('时间格式错误'); return false; }
  71. return Date.parse(new Date(year, month - 1, day, h, i, s)) / 1000;
  72. },
  73. // 过去时间计算
  74. fromTimer : function (time){
  75. var timer = (new Date()).getTime() - (time * 1000);
  76. timer = parseInt(timer / 1000);
  77. if(timer < 180){
  78. return '刚刚';
  79. }else if(timer >= 180 && timer < 3600){
  80. return parseInt(timer / 60) + '分钟前';
  81. }else if(timer >= 3600 && timer < 86400){
  82. return parseInt(timer / 3600) + '小时前';
  83. }else if(timer >= 86400 && timer < 2592000){
  84. return parseInt(timer / 86400) + '天前';
  85. }else{
  86. return this.formatDateTime(time, 'str');
  87. }
  88. }
  89. }