时间(年月日)总结 计算两个时间的时间差(天、小时、分钟、秒数)
1、获取当前时间
function getnowDate(type) { var date = new Date(); var seperator1 = "-"; var year = date.getFullYear(); var month = date.getMonth() + 1; var strDate = date.getDate(); var hour = date.getHours(); var minutes = date.getMinutes(); var seconds = date.getSeconds(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } if (hour >= 0 && hour <= 9) { hour = "0" + hour; } if (minutes >= 0 && minutes <= 9) { minutes = "0" + minutes; } if (seconds >= 0 && seconds <= 9) { seconds = "0" + seconds; } var currentdate = ''; if (type == 'year') { //年 currentdate = year; } else if (type == 'day') { //年月日 currentdate = year + seperator1 + month + seperator1 + strDate; } else if (type == 'seconds') { //年月日 时分秒 currentdate = year + seperator1 + month + seperator1 + strDate + " " + hour + ":" + minutes + ":" + seconds; }else if(type=="年"){ currentdate = year + '年' + month + '月' + strDate + '日' + " " + hour + ":" + minutes + ":" + seconds; } return currentdate; }
2、计算两个时间相差(天、小时、分钟、秒数)的函数
function GetDateDiff(startTime,endTime,diffType){ startTime=startTime.replace(/\-/g,"/"); endTime=endTime.replace(/\-/g,"/"); diffType=diffType.toLowerCase(); var sTime = new Date(startTime); //开始时间 var eTime = new Date(endTime); //结束时间</font> //作为除数的数字 var divNum = 1; switch (diffType){ case "second": divNum=1000; break; case "minute": divNum=1000*60; break; case "hour": divNum=1000*3600; break; case "day": divNum=1000*3600*24; break; default: break; } return parseInt((eTime.getTime()-sTime.getTime())/parseInt(divNum)); }
调用函数 计算相差天数
var ti = GetDateDiff('2019-10-09 19:00:00','2019-12-12 14:33:30','day'); //获取两个时间相差天数
3、几天前
function getDateDiff(hours){ var minute = 1000 * 60; var hour = minute * 60; var day = hour * 24; var halfamonth = day * 15; var month = day * 30; var now = new Date().getTime(); var diffValue = now - hours; if(diffValue < 0){return;} var monthC =diffValue/month; var weekC =diffValue/(7*day); var dayC =diffValue/day; var hourC =diffValue/hour; var minC =diffValue/minute; if(monthC>=1){ result="" + parseInt(monthC) + "月前"; } else if(weekC>=1){ result="" + parseInt(weekC) + "周前"; } else if(dayC>=1){ result=""+ parseInt(dayC) +"天前"; } else if(hourC>=1){ result=""+ parseInt(hourC) +"小时前"; } else if(minC>=1){ result=""+ parseInt(minC) +"分钟前"; }else { result="刚刚"; } return result; }
4、时间戳转化为年月日
//将long时间格式转化为年月日标准格式 function dateFormatUtil(longTypeDate){ var dateTypeDate = ""; var date = new Date(); date.setTime(longTypeDate); dateTypeDate = date.getFullYear()+"年"+ getMonths(date)+"月"+getDays(date)+"日"; return dateTypeDate; } //返回 01-12 的月份值 function getMonths(date){ var month = ""; month = date.getMonth() + 1; //getMonth()得到的月份是0-11 if(month<10){ month = "0" + month; } return month; } //返回01-30的日期 function getDays(date){ var day = ""; day = date.getDate(); if(day<10){ day = "0" + day; } return day; }
5、时间转换格式
function getDateTimeStamp(dateStr){ return Date.parse(dateStr.replace(/-/gi,"/")); }
6、秒数转化为时分秒格式
function formatSeconds(value) { var theTime = parseInt(value); // 秒 var middle = 0; // 分 var hour = 0; // 小时 if (theTime > 60) { middle = parseInt(theTime / 60); theTime = parseInt(theTime % 60); if (middle > 60) { hour = parseInt(middle / 60); middle = parseInt(middle % 60); } } var result = "" + parseInt(theTime) + "秒"; if (middle > 0) { result = "" + parseInt(middle) + "分" + result; } if (hour > 0) { result = "" + parseInt(hour) + "小时" + result; } return result; }