笔记记录,jq合js我就不区分了,自己辨别吧
确定2个日期是否在一个区间内
time1:第一个日期,time2:第二个日期,m:相差的区间
function completeDate(time1 , time2 , m){
var diffyear = time2.getFullYear() - time1.getFullYear() ;
var diffmonth = diffyear * 12 + time2.getMonth() - time1.getMonth() ;
if(diffmonth < 0 ){
return false ;
}
var diffDay = time2.getDate() - time1.getDate() ;
if(diffmonth < m || (diffmonth == m && diffDay <= 0)){
if(diffmonth == m && diffDay == 0){
var timeA = time1.getHours()*3600+60*time1.getMinutes()+time1.getSeconds();
var timeB = time2.getHours()*3600+60*time2.getMinutes()+time2.getSeconds();
if(timeB-timeA > 0){
return false;
}
}
return true ;
}
return false ;
}
日期比较
var start = new Date((querydatestart).replace(/-/g,"/"));
var end = new Date((querydateend).replace(/-/g,"/"));
if(start>end){
alert('开始日期大于结束日期')
}
if(start<end){
alert('开始日期小于结束日期');
}
金额的格式化(优化负数格式化)
<body>
<button onclick="formatMoney(-123456789.89,2)">格式化金额</button>
<input value="" id="num" />
</body>
<script>
function formatMoney(s, n) { //s是传入的数字,n是要保留的小数位
n = n > 0 && n <= 20 ? n : 2;
s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
let l = s.split(".")[0].split("").reverse();
let r = s.split(".")[1];
let t = "";
//新加逻辑
//==================
let f = "";
if (l.indexOf('-') > 0) {
f = l.pop();//l数组,原数负号在第一位,截取后在数组最后,移除他返回来给变量赋值
}
//===================
for (i = 0; i < l.length; i++) {
t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "")
}
let re = f+t.split("").reverse().join("") + "." + r;
document.getElementById("num").value = re;
}