var dateutil = {
/**
* 将指定字符串转换为日期
* @param date
* @returns {String}
*/
toDate: function (str) {
str.replace(/-/g, "/");
return new Date(str);
},
/**
* 将指定日期转换为字符串
* @param date
* @returns {String}
*/
toStr: function (date) {
var y = date.getFullYear();
var m = date.getMonth() + 1;
var d = date.getDate();
if (m < 10) m = "0" + m;
if (d < 10) d = "0" + d;
return y + "-" + m + "-" + d;
},
toDateEx: function (d) {
var datetime = new Date();
datetime.setTime(d);
var year = datetime.getFullYear();
var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
return year + "-" + month + "-" + date;
},
toDateTime: function (dtime) {
var datetime = new Date();
datetime.setTime(dtime);
var year = datetime.getFullYear();
var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
},
/**
* 将指定日期转换为字符串
* @param date
* @returns {String}
*/
toTimeStr: function (date) {
var y = date.getFullYear();
var m = date.getMonth() + 1;
var d = date.getDate();
var h = date.getHours();
var ms = date.getMinutes();
var s = date.getSeconds();
if (m < 10) m = "0" + m;
if (d < 10) d = "0" + d;
if (h < 10) h = "0" + h;
if (ms < 10) ms = "0" + ms;
if (s < 10) s = "0" + s;
return y + "-" + m + "-" + d + " " + h + ":" + ms + ":" + s;
},
/**
* 获得与某日期相隔几天的日期
* @param date --指定日期
* @param day --相隔天数(可正数负数)
* @returns
*/
intervalDay: function (date, day) {
var baseDate = new Date(Date.parse(date.replace(/-/g, "/")));
var baseYear = baseDate.getFullYear();
var baseMonth = baseDate.getMonth();
var baseDate = baseDate.getDate();
var newDate = new Date(baseYear, baseMonth, baseDate);
newDate.setDate(newDate.getDate() + day);
var temMonth = newDate.getMonth();
temMonth++;
var lastMonth = temMonth >= 10 ? temMonth : ("0" + temMonth)
var temDate = newDate.getDate();
var lastDate = temDate >= 10 ? temDate : ("0" + temDate)
newDate = newDate.getFullYear() + "-" + lastMonth + "-" + lastDate;
return newDate;
},
/**
* 获得某日期相隔多少月的日期
* @param month
* @returns {*}
*/
intervalMonth:function(date, month){
var y = date.getFullYear();
var m = date.getMonth() + 1;
var d = date.getDate();
m+=month;
if(m<1){
y=y+(parseInt(m/12)-1);
console.log("(m%12)="+(m%12));
m=(m%12)+12;
}else if(m>12){
y=y+parseInt(m/12);
m=m%12;
}
var d2= new Date(y,m,0);
d=Math.min(d,d2.getDate());
m=m<10?"0"+m:m;
d=d<10?"0"+d:d;
return y + "-" + m + "-" + d;
},
/**
* 获得某月的天数
* @param year --年
* @param month --月
* @returns
*/
monthDays: function (year, month) {
var startDate = new Date(year, month, 1);
var endDate = new Date(year, month + 1, 1);
return (endDate - startDate) / (1000 * 60 * 60 * 24);
},
/**
* 获得当前日期相隔几天的日期
* @param day --相隔天数(可正数负数)
* @returns
*/
addDay: function (day) {
return dateutil.intervalDay(dateutil.toStr(new Date()), day);
},
/**
* 获得当前日期相隔多少月的日期
* @param month
* @returns {*}
*/
addMonth:function (month) {
return dateutil.intervalMonth(new Date(),month);
},
/**
* 获间隔天数的星期几
* @param day
* @returns {Number}
*/
weekDayNumber: function (day) {
var week = new Date().getDay();
week = week == 0 ? 7 : week;
week += day;
return --week;
},
/**
* 获取本周一日期
* @returns
*/
mondayOfWeek: function () {
return dateutil.intervalDay(dateutil.toStr(new Date()), -dateutil.weekDayNumber(0));
},
/**
* 获取上周一日期
* @returns
*/
previousWeekday: function () {
return dateutil.intervalDay(dateutil.toStr(new Date()), -dateutil.weekDayNumber(7));
},
/**
* 获取上周日日期
* @returns
*/
previousWeekSunday: function () {
return dateutil.intervalDay(dateutil.toStr(new Date()), -dateutil.weekDayNumber(1));
},
/**
* 获取本月第一天日期
* @returns
*/
firstDayOfMonth: function () {
return dateutil.intervalDay(dateutil.toStr(new Date()), -new Date().getDate() + 1);
},
/**
* 获得与某月期相隔月数的日期
* @returns
*/
someMonthFirst: function (month) {
var now = new Date();
now.setMonth(now.getMonth() + month);
return dateutil.intervalDay(dateutil.toStr(now), -new Date().getDate() + 1);
},
/**
* 获取上月第一天日期
* @returns
*/
previousMonthFirst: function () {
return dateutil.someMonthFirst(-1);
},
/**
* 获取上月最后一天的日期
* @returns
*/
previousMonthEnd: function () {
var date = new Date(dateutil.someMonthFirst(-1))
return dateutil.intervalDay(dateutil.toStr(date), +(dateutil.monthDays(date.getYear(), date.getMonth()) - 1));
}
}
JS 日期工具库
最新推荐文章于 2024-04-07 20:25:47 发布