var dateControl = (function () {
/**
* 生成单个时间控件
* a:时间控件input的document对象
* b:保存获取到具体时间的document对象
*/
this.createDateControl = function (a, b, callback) {
a.daterangepicker({
singleDatePicker: true,
startDate: moment().subtract(0, 'days'),
autoUpdateInput: true,
showDropdowns: true,
locale: {
format: "YYYY-MM-DD",
daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
}
}).on('cancel.daterangepicker', function (ev, picker) {
a.val("请选择日期");
b.val("");
callback();
}).on('apply.daterangepicker', function (ev, picker) {
a.val(picker.startDate.format('YYYY-MM-DD'));
b.val(picker.startDate.format('YYYY-MM-DD'));
callback();
});;
}
/**
* 生成区间时间控件
* a:时间控件input的document对象
* b:保存获取到开始时间的document对象
* c:保存获取到结束时间的document对象
*/
this.doubleDateControl = function (a, b, c, callback) {
a.daterangepicker({
autoApply: false,
autoUpdateInput: false,
showDropdowns: true,
alwaysShowCalendars: false,
locale: {
format: "YYYY-MM-DD",
separator: " - ",
applyLabel: "确认",
cancelLabel: "清空",
fromLabel: "开始时间",
toLabel: "结束时间",
customRangeLabel: "自定义",
daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
}
}).on('cancel.daterangepicker', function (ev, picker) {
resetDate(a, b, c);
callback();
}).on('apply.daterangepicker', function (ev, picker) {
a.val(picker.startDate.format('YYYY-MM-DD') + " 至 " + picker.endDate.format('YYYY-MM-DD'));
b.val(picker.startDate.format('YYYY-MM-DD'));
c.val(picker.endDate.format('YYYY-MM-DD'));
callback();
});
function resetDate(a, b, c) {
var typeEle = $('#date_link_div a.current');
var type = typeEle.attr("value");
var startTime = b;
var endTime = c;
var today = formatDate(dateRangeUtil.getCurrentDate());
var week_start = formatDate(dateRangeUtil.getCurrentWeek()[0]);
var week_end = formatDate(dateRangeUtil.getCurrentWeek()[1]);
var month_start = formatDate(dateRangeUtil.getCurrentMonth()[0]);
var month_end = formatDate(dateRangeUtil.getCurrentMonth()[1]);
var year_start = formatDate(dateRangeUtil.getCurrentYear()[0]);
var year_end = formatDate(dateRangeUtil.getCurrentYear()[1]);
var total = "";
a.val("请选择日期范围");
if (type == "t") {
startTime.val(today);
endTime.val(today);
} else if (type == "w") {
startTime.val(week_start);
endTime.val(week_end);
} else if (type == "m") {
startTime.val(month_start);
endTime.val(month_end);
} else if (type == "y") {
startTime.val(year_start);
endTime.val(year_end);
}
}
}
return this;
})();
/**
* 日期范围工具类
*/
var dateRangeUtil = (function () {
/***
* 获得当前时间
*/
this.getCurrentDate = function () {
return new Date();
};
/***
* 获得昨天时间
*/
this.getYesterdayDate = function () {
var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
return yesterday;
};
/***
* 获得本周起止时间
*/
this.getCurrentWeek = function () {
//起止日期数组
var startStop = new Array();
//获取当前时间
var currentDate = this.getCurrentDate();
//返回date是一周中的某一天
var week = currentDate.getDay();
//返回date是一个月中的某一天
var month = currentDate.getDate();
//一天的毫秒数
var millisecond = 1000 * 60 * 60 * 24;
//减去的天数
var minusDay = week != 0 ? week - 1 : 6;
//alert(minusDay);
//本周 周一
var monday = new Date(currentDate.getTime() - (minusDay * millisecond));
//本周 周日
var sunday = new Date(monday.getTime() + (6 * millisecond));
//添加本周时间
startStop.push(monday); //本周起始时间
//添加本周最后一天时间
startStop.push(sunday); //本周终止时间
//返回
return startStop;
};
/***
* 获得本月的起止时间
*/
this.getCurrentMonth = function () {
//起止日期数组
var startStop = new Array();
//获取当前时间
var currentDate = this.getCurrentDate();
//获得当前月份0-11
var currentMonth = currentDate.getMonth();
//获得当前年份4位年
var currentYear = currentDate.getFullYear();
//求出本月第一天
var firstDay = new Date(currentYear, currentMonth, 1);
//当为12月的时候年份需要加1
//月份需要更新为0 也就是下一年的第一个月
if (currentMonth == 11) {
currentYear++;
currentMonth = 0; //就为
} else {
//否则只是月份增加,以便求的下一月的第一天
currentMonth++;
}
//一天的毫秒数
var millisecond = 1000 * 60 * 60 * 24;
//下月的第一天
var nextMonthDayOne = new Date(currentYear, currentMonth, 1);
//求出上月的最后一天
var lastDay = new Date(nextMonthDayOne.getTime() - millisecond);
//添加至数组中返回
startStop.push(firstDay);
startStop.push(lastDay);
//返回
return startStop;
};
/**
* 得到本季度开始的月份
* aram month 需要计算的月份
***/
this.getQuarterSeasonStartMonth = function (month) {
var quarterMonthStart = 0;
var spring = 0; //春
var summer = 3; //夏
var fall = 6; //秋
var winter = 9; //冬
//月份从0-11
if (month < 3) {
return spring;
}
if (month < 6) {
return summer;
}
if (month < 9) {
return fall;
}
return winter;
};
/**
* 获得该月的天数
* param year年份
* param month月份
* */
this.getMonthDays = function (year, month) {
//本月第一天 1-31
var relativeDate = new Date(year, month, 1);
//获得当前月份0-11
var relativeMonth = relativeDate.getMonth();
//获得当前年份4位年
var relativeYear = relativeDate.getFullYear();
//当为12月的时候年份需要加1
//月份需要更新为0 也就是下一年的第一个月
if (relativeMonth == 11) {
relativeYear++;
relativeMonth = 0;
} else {
//否则只是月份增加,以便求的下一月的第一天
relativeMonth++;
}
//一天的毫秒数
var millisecond = 1000 * 60 * 60 * 24;
//下月的第一天
var nextMonthDayOne = new Date(relativeYear, relativeMonth, 1);
//返回得到上月的最后一天,也就是本月总天数
return new Date(nextMonthDayOne.getTime() - millisecond).getDate();
};
/**
* 获得本季度的起止日期
*/
this.getCurrentSeason = function () {
//起止日期数组
var startStop = new Array();
//获取当前时间
var currentDate = this.getCurrentDate();
//获得当前月份0-11
var currentMonth = currentDate.getMonth();
//获得当前年份4位年
var currentYear = currentDate.getFullYear();
//获得本季度开始月份
var quarterSeasonStartMonth = this.getQuarterSeasonStartMonth(currentMonth);
//获得本季度结束月份
var quarterSeasonEndMonth = quarterSeasonStartMonth + 2;
//获得本季度开始的日期
var quarterSeasonStartDate = new Date(currentYear, quarterSeasonStartMonth, 1);
//获得本季度结束的日期
var quarterSeasonEndDate = new Date(currentYear, quarterSeasonEndMonth, this.getMonthDays(currentYear, quarterSeasonEndMonth));
//加入数组返回
startStop.push(quarterSeasonStartDate);
startStop.push(quarterSeasonEndDate);
//返回
return startStop;
};
/***
* 得到本年的起止日期
*
*/
this.getCurrentYear = function () {
//起止日期数组
var startStop = new Array();
//获取当前时间
var currentDate = this.getCurrentDate();
//获得当前年份4位年
var currentYear = currentDate.getFullYear();
//本年第一天
var currentYearFirstDate = new Date(currentYear, 0, 1);
//本年最后一天
var currentYearLastDate = new Date(currentYear, 11, 31);
//添加至数组
startStop.push(currentYearFirstDate);
startStop.push(currentYearLastDate);
//返回
return startStop;
};
/**
* 返回上一个月的第一天Date类型
* param year 年
* param month 月
**/
this.getPriorMonthFirstDay = function (year, month) {
//年份为0代表,是本年的第一月,所以不能减
if (month == 0) {
month = 11; //月份为上年的最后月份
year--; //年份减1
return new Date(year, month, 1);
}
//否则,只减去月份
month--;
return new Date(year, month, 1);;
};
/**
* 获得上一月的起止日期
* ***/
this.getPreviousMonth = function () {
//起止日期数组
var startStop = new Array();
//获取当前时间
var currentDate = this.getCurrentDate();
//获得当前月份0-11
var currentMonth = currentDate.getMonth();
//获得当前年份4位年
var currentYear = currentDate.getFullYear();
//获得上一个月的第一天
var priorMonthFirstDay = this.getPriorMonthFirstDay(currentYear, currentMonth);
//获得上一月的最后一天
var priorMonthLastDay = new Date(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth(), this.getMonthDays(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth()));
//添加至数组
startStop.push(priorMonthFirstDay);
startStop.push(priorMonthLastDay);
//返回
return startStop;
};
/**
* 获得上一周的起止日期
* **/
this.getPreviousWeek = function () {
//起止日期数组
var startStop = new Array();
//获取当前时间
var currentDate = this.getCurrentDate();
//返回date是一周中的某一天
var week = currentDate.getDay();
//返回date是一个月中的某一天
var month = currentDate.getDate();
//一天的毫秒数
var millisecond = 1000 * 60 * 60 * 24;
//减去的天数
var minusDay = week != 0 ? week - 1 : 6;
//获得当前周的第一天
var currentWeekDayOne = new Date(currentDate.getTime() - (millisecond * minusDay));
//上周最后一天即本周开始的前一天
var priorWeekLastDay = new Date(currentWeekDayOne.getTime() - millisecond);
//上周的第一天
var priorWeekFirstDay = new Date(priorWeekLastDay.getTime() - (millisecond * 6));
//添加至数组
startStop.push(priorWeekFirstDay);
startStop.push(priorWeekLastDay);
return startStop;
};
/**
* 得到上季度的起始日期
* year 这个年应该是运算后得到的当前本季度的年份
* month 这个应该是运算后得到的当前季度的开始月份
* */
this.getPriorSeasonFirstDay = function (year, month) {
var quarterMonthStart = 0;
var spring = 0; //春
var summer = 3; //夏
var fall = 6; //秋
var winter = 9; //冬
//月份从0-11
switch (month) { //季度的其实月份
case spring:
//如果是第一季度则应该到去年的冬季
year--;
month = winter;
break;
case summer:
month = spring;
break;
case fall:
month = summer;
break;
case winter:
month = fall;
break;
};
return new Date(year, month, 1);
};
/**
* 得到上季度的起止日期
* **/
this.getPreviousSeason = function () {
//起止日期数组
var startStop = new Array();
//获取当前时间
var currentDate = this.getCurrentDate();
//获得当前月份0-11
var currentMonth = currentDate.getMonth();
//获得当前年份4位年
var currentYear = currentDate.getFullYear();
//上季度的第一天
var priorSeasonFirstDay = this.getPriorSeasonFirstDay(currentYear, currentMonth);
//上季度的最后一天
var priorSeasonLastDay = new Date(priorSeasonFirstDay.getFullYear(), priorSeasonFirstDay.getMonth() + 2, this.getMonthDays(priorSeasonFirstDay.getFullYear(), priorSeasonFirstDay.getMonth() + 2));
//添加至数组
startStop.push(priorSeasonFirstDay);
startStop.push(priorSeasonLastDay);
return startStop;
};
/**
* 得到去年的起止日期
* **/
this.getPreviousYear = function () {
//起止日期数组
var startStop = new Array();
//获取当前时间
var currentDate = this.getCurrentDate();
//获得当前年份4位年
var currentYear = currentDate.getFullYear();
currentYear--;
var priorYearFirstDay = new Date(currentYear, 0, 1);
var priorYearLastDay = new Date(currentYear, 11, 1);
//添加至数组
startStop.push(priorYearFirstDay);
startStop.push(priorYearLastDay);
return startStop;
};
/**
* 获取指定的日期
*/
this.getSpecificData = function (aa) {
var date1 = new Date(),
time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate(); //time1表示当前时间
var date2 = new Date(date1);
date2.setDate(date1.getDate() + aa);
return date2;
}
return this;
})();
function formatDate(date) {
var myyear = date.getFullYear();
var mymonth = date.getMonth() + 1;
var myweekday = date.getDate();
if (mymonth < 10) {
mymonth = "0" + mymonth;
}
if (myweekday < 10) {
myweekday = "0" + myweekday;
}
return (myyear + "-" + mymonth + "-" + myweekday);
}
function getDateHours() { // 获取当前时间往后多少个小时方法
var time = ""
var oneDay = ""
var date = ""
date = new Date();
time = date.getTime(); //当前的毫秒数
oneDay = 1000 * 60 * 60 // 1小时多少毫秒
date.setTime(time + oneDay * 2); // 2是往后的多少个小时
myGetDate(date)
}
function myGetDate(d) { // 格式化时间 YYYY-MM-DD HH:mm
return `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()} ${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}`;
}
日期常用方法
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...