js常用工具Api归纳【起始于2021-01-17】
2021-01-29 本文已影响0人
鄙人_阿K
前端文章汇总
1、时间字符串相减得出天数
// 时间字符串相减
function dateMinus(date1, date2) {
//date1:小日期 date2:大日期
var sdate = new Date(date1);
var now = new Date(date2);
var days = now.getTime() - sdate.getTime();
var day = parseInt(days / (1000 * 60 * 60 * 24));
return day;
}
2、多选框如何充当单选框使用?(jquery支持)
// 设置单选框
$("input:checkbox").click(function () {
var boxArray = document.getElementsByName(this.name);
for (var i = 0; i <= boxArray.length - 1; i++) {
if (boxArray[i] == this && this.checked) {
boxArray[i].checked = true;
} else {
boxArray[i].checked = false;
}
}
});
3、获取当前时间【格式一:年月日,格式二:年月日-时分秒】
//获取当前日期,格式YYYY-MM-DD
function getNowFormatDay() {
var char = "-";
var nowDate = new Date();
var day = nowDate.getDate();
var month = nowDate.getMonth() + 1;//注意月份需要+1
var year = nowDate.getFullYear();
//补全0,并拼接
return year + char + completeDate(month) + char + completeDate(day);
}
//获取当前时间,格式YYYY-MM-DD HH:mm:ss
function getNowFormatTime() {
var nowDate = new Date();
var colon = ":";
var h = nowDate.getHours();
var m = nowDate.getMinutes();
var s = nowDate.getSeconds();
//补全0,并拼接
return getNowFormatDay(nowDate) + " " + completeDate(h) + colon + completeDate(m) + colon + completeDate(s);
}
//补全0
function completeDate(value) {
return value < 10 ? "0" + value : value;
}
4、半年后日期
// 半年后
function getNowFormatDay2() {
var char = "-";
var nowDate = new Date();
nowDate.setMonth(nowDate.getMonth() + 6); //加半年
var day = nowDate.getDate();
var month = nowDate.getMonth() + 1;//注意月份需要+1
var year = nowDate.getFullYear();
//补全0,并拼接
return year + char + completeDate(month) + char + completeDate(day);
}
5、月末
/**
* 判断是否为当月的最后一天
*/
function isLastDayOfMonth() {
var flag = new Boolean(false);
var date = new Date();
var year = date.getFullYear();
var month2 = date.getMonth()+1;
var month = date.getMonth()+1;
var today = date.getDate();
var new_year = year; //取当前的年份
var new_month = month++;//取下一个月的第一天,方便计算(最后一天不固定)
if(month>12){//如果当前大于12月,则年份转到下一年
new_month -=12; //月份减
new_year++; //年份增
}
var new_date = new Date(new_year,new_month,1); //取当年当月中的第一天
var month_last_day = (new Date(new_date.getTime()-1000*60*60*24)).getDate();
// 当前时间
var resultTime1 =year+"-"+month2+"-"+today;
// 月底时间
var resultTime2 = new_year+"-"+new_month+"-"+month_last_day;
if(resultTime1 == resultTime2){
flag = true;
}
return flag;
}
6、下月第一天 和 下月最后一天
/*获取下个月的第一天*/
function nextMonthFirstDay() {
var time = new Date();
var year = time.getFullYear();
var month = time.getMonth() + 2;
if (month > 12) {
month = month - 12;
year = year + 1;
}
var day = 1;
return year + '-' + completeDate(month) + '-' + completeDate(day);
}
/*获取下个月的最后一天*/
function nextMonthLastDay() {
var time = new Date();
var year = time.getFullYear();
var month = time.getMonth() + 2;
if (month > 12) {
month = month - 12;
year = year + 1;
}
var day = this.nextMonthDay(year, month);
return year + '-' + completeDate(month) + '-' + completeDate(day);
}
function nextMonthDay(year, month) {//判断每月多少天
var day31 = [1, 3, 5, 7, 8, 10, 12];
var day30 = [4, 6, 9, 11];
if (day31.indexOf(month) > -1) {
return 31;
} else if (day30.indexOf(month) > -1) {
return 30;
} else {
if (this.isLeapYear(year)) {
return 29;
} else {
return 28;
}
}
}
function isLeapYear(year) {//判断是否为闰年
return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
}
//补全0
function completeDate(value) {
return value < 10 ? "0" + value : value;
}