获取上、下,周、天、年的时间

// 金额格式化
export const formatDate = (fmt) => {
const date = new Date()
const o = {
'Y+': date.getFullYear(),
'M+': date.getMonth() + 1, // 月
'D+': date.getDate(), // 日
'h+': date.getHours(), // 时
'm+': date.getMinutes(), // 分
's+': date.getSeconds(), // 秒
W: date.getDay() // 周
}
for (let k in o) {
if (new RegExp('(' + k + ')').test(fmt)) {
fmt = fmt.replace(RegExp.$1, () => {
if (k === 'W') {
// 星期几
const week = ['日', '一', '二', '三', '四', '五', '六']
return week[o[k]]
} else if (k === 'Y+' || RegExp.$1.length === 1) {
// 年份 or 小于10不加0
return o[k]
} else {
return ('00' + o[k]).substr(('' + o[k]).length) // 小于10补位0
}
})
}
}
return fmt
}
export function formats(date) {
var date = new Date(date);
var YY = date.getFullYear() + '-';
var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
return YY + MM + DD + " " + hh + mm + ss;
}
/**
 * 日期范围工具类
 */
export function startTime(time) {
const nowTimeDate = new Date(time)
return nowTimeDate.setHours(0, 0, 0, 0)
}
export function endTime(time) {
const nowTimeDate = new Date(time)
return nowTimeDate.setHours(23, 59, 59, 999)
}
/***
 * 当前时间
 */
export function getCurrentDate() {
return new Date();
}
/***
 * 今天的开始时间
 */
export function getBeginToday() {
return new Date(new Date(new Date().toLocaleDateString()).getTime());
}
/***
 * 昨天开始时间
 */
export function getBeginYesterday() {
return startTime(getBeginToday() - 24 * 60 * 60 * 1000);
}
/***
 * 昨天结束时间时间
 */
export function getEndYesterday() {
return endTime(getBeginToday() - 24 * 60 * 60 * 1000);
}
/***
 * 本周的第一天时间
 */
export function getBeginWeek() {
var currentDate = getCurrentDate();
var week = currentDate.getDay();
//一天的毫秒数
var millisecond = 1000 * 60 * 60 * 24;
//减去的天数
var minusDay = week != 0 ? week - 1 : 6;
//本周 周一
var monday = new Date(currentDate.getTime() - (minusDay * millisecond));
return startTime(monday);
}
/***
 * 本周的最后一天时间
 */
export function getEndWeek() {
var currentDate = getCurrentDate();
var week = currentDate.getDay();
//一天的毫秒数
var millisecond = 1000 * 60 * 60 * 24;
//减去的天数
var minusDay = week != 0 ? week - 1 : 6;
//本周 周日
var monday = new Date(currentDate.getTime() - (minusDay * millisecond));
var sunday = new Date(monday.getTime() + (6 * millisecond));
//返回
return endTime(sunday);
}
/***
 * 上周的开始
 */
export function getBeginLastWeek() {
var currentDate = getCurrentDate();
var first = currentDate.getDate() - currentDate.getDay() - 6;
var startDate = new Date(currentDate.setDate(first));
return startTime(startDate);
}
/***
 * 上周的结束
 */
export function getEndLastWeek() {
var currentDate = getCurrentDate();
var first = currentDate.getDate() - currentDate.getDay() - 6;
var last = first + 6;
var endDate = new Date(currentDate.setDate(last));
return endTime(endDate);
}
/***
 * 本月的第一天时间
 */
export function getBeginMonth() {
var currentDate = getCurrentDate();
var currentMonth = currentDate.getMonth();
//获得当前年份4位年
var currentYear = currentDate.getFullYear();
//求出本月第一天
var firstDay = new Date(currentYear, currentMonth, 1);
return firstDay;
};
/***
 * 本月的最后一天时间
 */
export function getEndMonth() {
//获取当前时间
var currentDate = getCurrentDate();
var fullYear = currentDate.getFullYear();
var month = currentDate.getMonth() + 1; // getMonth 方法返回 0-11,代表1-12月
var endOfMonth = new Date(fullYear, month, 0);
return endTime(endOfMonth);
};
/***
 * 上月的第一天时间
 */
export function getBeginLastMonth() {
//获取当前时间
var currentDate = getCurrentDate();
//获得当前月份0-11
var currentMonth = currentDate.getMonth();
//获得当前年份4位年
var currentYear = currentDate.getFullYear();
//获得上一个月的第一天
var priorMonthFirstDay = getPriorMonthFirstDay(currentYear, currentMonth);
return priorMonthFirstDay;
};
/***
 * 上月的最后一天时间
 */
export function getEndLastMonth() {
//获取当前时间
var currentDate = getCurrentDate();
//获得当前月份0-11
var currentMonth = currentDate.getMonth();
//获得当前年份4位年
var currentYear = currentDate.getFullYear();
//当为12月的时候年份需要加1
//月份需要更新为0 也就是下一年的第一个月
if (currentMonth == 11) {
currentYear++;
currentMonth = 0; //就为
} else {
//否则只是月份增加,以便求的下一月的第一天
currentMonth++;
}
//一天的毫秒数
var millisecond = 1000 * 60 * 60 * 24;
//求出上月的最后一天
var lastDay = new Date(getBeginMonth().getTime() - millisecond);
return endTime(lastDay);
};
/**
 * 返回上一个月的第一天Date类型
 * @param year 年
 * @param month 月
 **/
export function getPriorMonthFirstDay(year, month) {
//年份为0代表,是本年的第一月,所以不能减
if (month == 0) {
month = 11; //月份为上年的最后月份
year--; //年份减1
return new Date(year, month, 1);
}
//否则,只减去月份
month--;
return new Date(year, month, 1);;
};
// 获取上一个月
export function getLastMonthDate(params) {
var date = params ? new Date(params.replace(/-/g, '/')) : new Date()
var daysInMonth = new Array([0], [31], [28], [31], [30], [31], [30], [31], [31], [30], [31], [30], [31])
var strYear = date.getFullYear()
var strDay = date.getDate()
var strMonth = date.getMonth() + 1
var monthday = daysInMonth[strMonth - 1][0]
if (strYear % 4 == 0 && strYear % 100 != 0) {
daysInMonth[2] = 29
}
if (strMonth - 1 == 0) {
strYear -= 1
strMonth = 12
} else {
strMonth -= 1
}
strDay = daysInMonth[strMonth] >= strDay ? strDay : daysInMonth[strMonth]
if (strMonth < 10) {
strMonth = '0' + strMonth
}
if (strDay < 10) {
strDay = '0' + strDay
}
var datastr = strYear + '-' + strMonth + '-' + '01'
var dataend = strYear + '-' + strMonth + '-' + monthday
var time = {
'start': datastr,
"end": dataend
}
return time
};
// 获取下一个月
export function getNextMonthDate(params) {
var date = params ? new Date(params.replace(/-/g, '/')) : new Date()
var daysInMonth = new Array([0], [31], [28], [31], [30], [31], [30], [31], [31], [30], [31], [30], [31])
var strYear = date.getFullYear()
var strDay = date.getDate()
var strMonth = date.getMonth() + 1
if (strYear % 4 == 0 && strYear % 100 != 0) {
daysInMonth[2] = 29
}
if (strMonth + 1 == 13) {
strYear += 1
strMonth = 1
} else {
strMonth += 1
}
var monthday = daysInMonth[strMonth][0]
strDay = daysInMonth[strMonth] >= strDay ? strDay : daysInMonth[strMonth]
if (strMonth < 10) {
strMonth = '0' + strMonth
}
if (strDay < 10) {
strDay = '0' + strDay
}
var datastr = strYear + '-' + strMonth + '-' + strDay
var dataend = strYear + '-' + strMonth + '-' + monthday
var time = {
'start': datastr,
"end": dataend,
'mon': strMonth
}
return time
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值