JS常用函数

function allWeeks(now_month) {
//获取每个月的周
let week_array = [];
let today = new Date(Date.parse(now_month));
let year = today.getFullYear();
let month = today.getMonth();
let i = 0;

let start = new Date(year, month, 1); // 得到当月第一天
let end = new Date(year, month + 1, 0); // 得到当月最后一天
let start_day = start.getDay(); // 当月第一天是周几
console.log(start_day, start.format("yyyy-MM-dd"), end.format("yyyy-MM-dd")); // 每月的起始日期
switch (start_day) {
case 0: i = 0 - 5; break;
case 1: i = 1; break;
case 2: i = 0 - 0; break;
case 3: i = 0 - 1; break;
case 4: i = 0 - 2; break;
case 5: i = 0 - 3; break;
case 6: i = 0 - 4; break;
}
/* switch (start_day) {
case 0: i = 0 - 1; break;
case 1: i = 0 - 2; break;
case 2: i = 0 - 3; break;
case 3: i = 0 - 4; break;
case 4: i = 0 - 5; break;
case 5: i = 1; break;
case 6: i = 0; break;
}*/
let end_day = end.getDay()//当月最后一天是周几
if (end_day != 0) {
end = new Date(year, month + 1, (7 - end_day)) //获得最后一周的星期日 可能跨月
}

console.log(end)
while (new Date(year, month, i + 6) <= end) {

week_array.push([new Date(year, month, i).format("yyyy-MM-dd"),
new Date(year, month, i + 6).format("yyyy-MM-dd")
]
)
i += 7;
}

console.log(week_array);
return week_array;
}
function ChangeDateFormat(val) {
//格式化"/Date(1555472797293)/"
if (val != null) {
var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));
//月份为0-11,所以+1,月份小于10时补个0
var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
return date.getFullYear() + "." + month + "." + currentDate;
}
return "";
}
function ChangeDateFormat2(val) {
//"/Date(1555472797293)/"
var date = null
if (val != null) {
date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));
} else {
date = new Date()
}
//月份为0-11,所以+1,月份小于10时补个0
var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
return date.getFullYear() + "-" + month + "-" + currentDate;

return "";
}
//保留2位小数 
function formatMoney(x, type) {
var f = parseFloat(x);

if (isNaN(f)) {

return false;

}

var f = Math.round(x * 100) / 100;

var s = f.toString();

var rs = s.indexOf('.');

if (rs < 0) {

rs = s.length;

s += '.';

}

while (s.length <= rs + 2) {

s += '0';

}

return s;
//return Math.floor(s * 100) / 100
/* if (/[^0-9\.]/.test(s))
return "0.00";
if (s == null || s == "null" || s == "")
return "0.00";
s = s.toString().replace(/^(\d*)$/, "$1.");
s = (s + "00").replace(/(\d*\.\d\d)\d* /, "$1");
s = s.replace(".", ",");
var re = /(\d)(\d{3},)/;
while (re.test(s))
s = s.replace(re, "$1,$2");
s = s.replace(/,(\d\d)$/, ".$1");
if (type == 0) {
var a = s.split(".");
if (a[1] == "00") {
s = a[0];
}
}
return s;*/
}

 

posted @ 2019-05-06 17:26  羽鱼  阅读(147)  评论(0编辑  收藏  举报