获取时间差 (当前系统时间 - 后台返回的时间)

  1. //获取时间差 (当前系统时间)2019-09-27 18:42:47 减去 2019-09-27 16:42:47
  2. function getTime(time){
  3. var times = time_year='',time_month='',time_day='',time_hour='';
  4. var date = new Date();
  5. var year = date.getFullYear();
  6. var month = date.getMonth()+1;
  7. var day = date.getDate();
  8. var hour = date.getHours();
  9. var minute = date.getMinutes();
  10. var second = date.getSeconds();
  11. //console.log(year+'-'+month+'-'+day+' -'+hour+':'+minute+':'+second);
  12. times = time.split('-');
  13. time_year = times[0];//年
  14. time_month = times[1];//月
  15. time_day = times[2].split(' ')[0];//日
  16. time_hour = times[2].split(' ')[1];
  17. time_hour = time_hour.split(':')[0];//时
  18. time_year = year - time_year
  19. time_month = month - time_month;
  20. time_day = day - time_day;
  21. time_hour = hour - time_hour;
  22. if(time_year >0){
  23. return time_year+'年'+time_month+'月'+time_day+'天'+time_hour+'小时'
  24. }else{
  25. if(time_month >0){
  26. return time_month+'月'+time_day+'天'+time_hour+'小时'
  27. }else{
  28. if(time_day >0){
  29. return time_day+'天'+time_hour+'小时'
  30. }else{
  31. return time_hour+'小时前'
  32. }
  33. }
  34. }
  35. }

日期拼接格式化为 9月12号 星期四 09:10形式

  1. //日期拼接格式化为 9月12号 星期四 09:10形式
  2. //month:月份 data:几号 day:数字1-7
  3. function matchDatas(month,date,day){
  4. var datas ='',re_month='01',re_data = '30',re_day="1";
  5. var re_month= month +1;
  6. var re_data= date;
  7. var re_day= day;
  8. if(re_day == '1'){
  9. re_day ='星期一';
  10. }else if(re_day == '2'){
  11. re_day ='星期二';
  12. }else if(re_day == '3'){
  13. re_day ='星期三';
  14. }else if(re_day == '4'){
  15. re_day ='星期四';
  16. }else if(re_day == '5'){
  17. re_day ='星期五';
  18. }else if(re_day == '6'){
  19. re_day ='星期六';
  20. }else{
  21. re_day ='星期日';
  22. }
  23. datas = re_month + '月' + re_data +'号  '+ re_day;
  24. return datas
  25. }

时间拼接如:2019-10-09 17:24:11.0与 19:00换成 2019-10-09 19:00再加45分钟

  1. //时间拼接2019-10-09 17:24:11.0与 19:00换成 2019-10-09 19:00再加45分钟
  2. function cutOffTime(creattime,starttime){
  3. var datas='',clock='',minute=''
  4. if(starttime.indexOf('-') == -1){
  5. datas = creattime.split(' ')[0];//得到2019-10-09
  6. minute = +starttime.split(':')[1] + 45; //分钟先转成数字,再加45
  7. if(minute >60){ //如果大于60分钟,时钟+1
  8. minute = minute - 60;
  9. clock = +starttime.split(':')[0] + 1;
  10. }else{
  11. clock = +starttime.split(':')[0];
  12. }
  13. return datas+' '+clock+':'+minute
  14. //return clock+':'+minute
  15. }else{
  16. datas= starttime.split(' ')[0]; //得到2019-10-09
  17. var clock_minute = starttime.split(' ')[1]; //15:32:25
  18. minute = +clock_minute.split(':')[1] + 45;//分钟先转成数字,再加45
  19. if(minute >60){ //如果大于60分钟,时钟+1
  20. minute = minute - 60;
  21. clock = +clock_minute.split(':')[0] + 1;
  22. }else{
  23. clock = +starttime.split(':')[0];
  24. }
  25. return datas+' '+clock+':'+minute
  26. }
  27. }

毫秒转时分秒1

  1. //毫秒转时分秒
  2. function changeTime(time){
  3. var days = time / 1000 / 60 / 60 / 24;
  4. var daysRound = Math.floor(days);
  5. var hours = time/ 1000 / 60 / 60 - (24 * daysRound);
  6. var hoursRound = Math.floor(hours);
  7. var minutes = time / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound);
  8. var minutesRound = Math.floor(minutes);
  9. var seconds = time/ 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
  10. if(daysRound == 0){
  11. if(hoursRound == 0){
  12. return '刚刚'
  13. }
  14. return hoursRound +'小时前'
  15. }
  16. return daysRound +'天'+ hoursRound +'小时前'
  17. }

毫秒转时分秒2

  1. var times = 1566354720000
  2. changeTime(times);
  3. //毫秒转时分秒
  4. function changeTime(time){
  5. var days = time / 1000 / 60 / 60 / 24;
  6. var daysRound = Math.floor(days);
  7. var hours = time/ 1000 / 60 / 60 - (24 * daysRound);
  8. var hoursRound = Math.floor(hours);
  9. var minutes = time / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound);
  10. var minutesRound = Math.floor(minutes);
  11. var seconds = time/ 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
  12. if(daysRound == 0){
  13. if(hoursRound == 0){
  14. if(minutesRound == 0){
  15. return seconds +'秒'
  16. }
  17. return minutesRound +'分'+ seconds +'秒'
  18. }
  19. return hoursRound +'小时'+ minutesRound +'分'+ seconds +'秒'
  20. }
  21. return daysRound +'天'+ hoursRound +'小时'+ minutesRound +'分'+ seconds +'秒'
  22. }

时间比较(这种写法不太好,更优的做法是转成时间戳再比较new Date().getTime())

  1. //时间比较 //排除了两个时间是同一天的
  2. comparisonTime(createTime, nowTime, message) {
  3. if (nowTime != '') {
  4. let createDate = createTime.toString()
  5. var stringDate = new Date(createDate);
  6. var arrDate = [stringDate.getFullYear(), stringDate.getMonth() + 1, stringDate.getDate()];
  7. for (var i = 0, len = arrDate.length; i < len; i++) {
  8. if (arrDate[i] < 10) {
  9. arrDate[i] = '0' + arrDate[i];
  10. }
  11. }
  12. let purcharr = nowTime.split('-');
  13. if (purcharr[0] >= arrDate[0]) { //年
  14. if (purcharr[1] >= arrDate[1]) { //月
  15. if (purcharr[2] >= arrDate[2]) { //日
  16. return true;
  17. } else {
  18. this.$message({
  19. showClose: true,
  20. message: message,
  21. type: 'error'
  22. });
  23. return false;
  24. }
  25. } else {
  26. this.$message({
  27. showClose: true,
  28. message: message,
  29. type: 'error'
  30. });
  31. return false;
  32. }
  33. } else {
  34. this.$message({
  35. showClose: true,
  36. message: message,
  37. type: 'error'
  38. });
  39. return false;
  40. }
  41. }
  42. },

校验时间(座机和手机号都会校验)

  1. checkTel(val, dataType) {
  2. var mobilevalid = /^((0\d{2,3}-\d{7,8})|(1[3|4|5|7|8]\d{9}))$/;
  3. if (!mobilevalid.test(val)) {
  4. this.$notify({
  5. type: MsgType.INFO,
  6. message: '请输入正确的联系电话!'
  7. })
  8. return false;
  9. }
  10. },