1.判断是否为同一天
function isSameFay(t) { return new Date(t).toDateString() === new Date().toDateString(); }
2. 将时间字符串转换为时间戳NAN
var data = "2019-01-22 11:11:11"; //将"-"换成"/",IOS不支持‘-’ var time = data.replace(/-/g, '/'); //转换成时间戳 time = new Date(time).getTime()
3.时间戳转化为字符串
function timestampTransformStrDate(timestamp, type){ //type 1 XX-XX-XX XX:XX:XX; 2 XX月XX日XX:XX var myDate = timestamp || new Date(); //获取当前年 var year = myDate.getFullYear(); //获取当前月 var month = myDate.getMonth() + 1; //获取当前日 var day = myDate.getDate(); var h = myDate.getHours(); //获取当前小时数(0-23) var m = myDate.getMinutes(); //获取当前分钟数(0-59) var s = myDate.getSeconds(); //获取当前时间 var now if (type == 1) { now = year + '-' + conver(month) + "-" + conver(day) + " " + conver(h) + ':' + conver(m) + ":" + conver(s); } else if (type == 2) { now = conver(month) + "月" + conver(day) + "日" + conver(h) + ':' + conver(m); } return now; } //日期时间处理 function conver(s) { return s < 10 ? '0' + s : s; }
4.字符串转化为时间
function getDateFromString(dateString) { var parts = dateString.split(' '); if(parts.length < 1) { return null; } var part1 = parts[0].split('-'); if(part1.length != 3) { return null; } var d = new Date(part1[0], part1[1] - 1, part1[2]); if(parts[1] && d) { var part2 = parts[1].split(':'); if(part2) { d.setHours(part2[0]); d.setMinutes(part2[1]); d.setSeconds(part2[2]); } } return d; }
5.比较日期大小
function compDate(date1, date2) { var a = Date.parse(date1) - Date.parse(date2); // console.log("a:"+a); if(a < 0) { // console.log("date1小!"); } else if(a > 0) { // console.log("date1大!"); } else if(a == 0) { // console.log("时间相等!"); } else { return 'exception'; } return a; }
6.设置剩余时间
var timeFlag = false var timeLeftTimer = setInterval(function() { setTimeLeft('2019-12-18 15:00:00') }, 1000) //设置剩余时间 function setTimeLeft(endTime) { endTimestamp = strDateTransformTimestamp(endTime) var timeLeft = endTimestamp - new Date().getTime() if (timeLeft <= 0) { timeFlag = true } var timeLeftD = parseInt(timeLeft / 86400000) //剩余天数 var timeLeftH = parseInt((timeLeft % 86400000) / 3600000) //剩余小时数 var timeLeftM = parseInt((timeLeft % 3600000) / 60000) //剩余分钟数 var timeLeftS = parseInt((timeLeft % 60000) / 1000) //剩余秒数 var timeLeftStr = timeLeftD + '天' + timeLeftH + '小时' + timeLeftM + '分' + timeLeftS + '秒' // console.log(timeLeftStr)if (timeFlag) { clearInterval(timeLeftTimer) } } //日期转换为时间戳 function strDateTransformTimestamp(str) { //str '2015-03-05 17:59:00' str = str.substring(0,19); str = str.replace(/-/g,'/'); return new Date(str).getTime(); }
7.获取当前时间格式化
function getDate(){ var myDate = new Date(); //获取当前年 var year = myDate.getFullYear(); //获取当前月 var month = myDate.getMonth() + 1; //获取当前日 var date = myDate.getDate(); var h = myDate.getHours(); //获取当前小时数(0-23) var m = myDate.getMinutes(); //获取当前分钟数(0-59) var s = myDate.getSeconds(); //获取当前时间 var now = year + '-' + conver(month) + "-" + conver(date) + " " + conver(h) + ':' + conver(m) + ":" + conver(s); return now; } //日期时间处理 function conver(s) { return s < 10 ? '0' + s : s; }
8.时间处理
function handleDate(date, isHasHMS, days) { //isHasHMS是否需要时分秒,date时间,days增加多少天 var myDate = '' if (date) { myDate = new Date(date) } else { myDate = new Date() } if (days == undefined || days == '') { days = 0 } myDate.setDate(myDate.getDate() + days) var year = myDate.getFullYear() //获取当前年 var month = myDate.getMonth() + 1 //获取当前月 var day = myDate.getDate() //获取当前日 var hour = myDate.getHours() //获取当前小时数(0-23) var minute = myDate.getMinutes() //获取当前分钟数(0-59) var second = myDate.getSeconds() //获取当前秒(0-59) //获取当前时间 if (isHasHMS) { date = year + '-' + this.conver(month) + "-" + this.conver(day) + " " + this.conver(hour) + ':' + this.conver(minute) + ":" + this.conver(second) } else { date = year + '-' + this.conver(month) + "-" + this.conver(day) } return date } conver(s) { return s < 10 ? '0' + s : s }