js 获取系统当前时间,判断时间大小
1、获取系统当前时间
getNowTime(tempminit) { if (!tempminit) { tempminit = 0; } var date = new Date(); date.setMinutes(date.getMinutes() - tempminit); var year = date.getFullYear(), month = date.getMonth() + 1, day = date.getDate(), hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(), minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(), second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); month >= 1 && month <= 9 ? (month = "0" + month) : ""; day >= 0 && day <= 9 ? (day = "0" + day) : ""; var timer = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second; /* console.log(timer); */ return timer; }
2、判断时间差
// 比较两个日期的大小 // startStr较大时为false, 否则为true , dateCompare(startStr, endStr) { var d1, d2, s, arr, arr1, arr2; if (startStr.length > 10) { arr = startStr.split(" "); arr1 = arr[0].split("-"); arr2 = arr[1].split(":"); d1 = new Date(arr1[0], arr1[1] - 1, arr1[2], arr2[0], arr2[1], arr2[2]); } else { arr = startStr.split("-"); d1 = new Date(arr[0], arr[1], arr[2]); } if (endStr.length > 10) { arr = endStr.split(" "); arr1 = arr[0].split("-"); arr2 = arr[1].split(":"); d2 = new Date(arr1[0], arr1[1] - 1, arr1[2], arr2[0], arr2[1], arr2[2]); } else { arr = endStr.split("-"); d2 = new Date(arr[0], arr[1], arr[2]); } s = d2 - d1; if (s < 0) { return false; } return true; }