123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- export const calcDate = (date1, date2) => {
- let date3 = date2 - date1;
- let days = Math.floor(date3 / (24 * 3600 * 1000))
- let leave1 = date3 % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
- let hours = Math.floor(leave1 / (3600 * 1000))
- let leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
- let minutes = Math.floor(leave2 / (60 * 1000))
- let leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
- let seconds = Math.round(date3 / 1000)
- return {
- leave1,
- leave2,
- leave3,
- days: days,
- hours: hours,
- minutes: minutes,
- seconds: seconds,
- }
- }
- /**
- * 日期格式化
- */
- export function dateFormat(date, format) {
- format = format || 'yyyy-MM-dd hh:mm:ss';
- if (date !== 'Invalid Date') {
- let o = {
- "M+": date.getMonth() + 1, //month
- "d+": date.getDate(), //day
- "h+": date.getHours(), //hour
- "m+": date.getMinutes(), //minute
- "s+": date.getSeconds(), //second
- "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
- "S": date.getMilliseconds() //millisecond
- }
- if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
- (date.getFullYear() + "").substr(4 - RegExp.$1.length));
- for (let k in o)
- if (new RegExp("(" + k + ")").test(format))
- format = format.replace(RegExp.$1,
- RegExp.$1.length === 1 ? o[k] :
- ("00" + o[k]).substr(("" + o[k]).length));
- return format;
- }
- return '';
- }
- /**
- * 获取上月第一天和下月最后一天
- * type 1为 [yyyy-MM-dd,yyyy-MM-dd]
- * type 2为 [yyyy-MM-dd 00:00:00,yyyy-MM-dd 23:59:59]
- * type为空默认2
- */
- export function defaultDate(type) {
- type = type ? type : 2
- const date = new Date();
- const startDate = new Date(date.getFullYear(), date.getMonth() - 1, 1);
- const endDate = new Date(date.getFullYear(), date.getMonth() + 2, 0);
- if (type == 1) {
- return [
- dateFormat(startDate, "yyyy-MM-dd"),
- dateFormat(endDate, "yyyy-MM-dd")
- ];
- }else if (type == 3){
- return[
- dateFormat(new Date(date.getFullYear(), date.getMonth(), date.getDate()), "yyyy-MM-dd"),
- dateFormat(new Date((date.getMonth() + 1) === 12?date.getFullYear()+1:date.getFullYear(), (date.getMonth() + 1), date.getDate()), "yyyy-MM-dd")
- ];
- }else {
- return [
- dateFormat(startDate, "yyyy-MM-dd") + " 00:00:00",
- dateFormat(endDate, "yyyy-MM-dd") + " 23:59:59"
- ];
- }
- }
- /**
- * 获取当天
- * [yyyy-MM-dd,yyyy-MM-dd]
- */
- export function defaultDate2() {
- const date = new Date();
- return [
- dateFormat(date, "yyyy-MM-dd"),
- dateFormat(date, "yyyy-MM-dd")
- ];
- }
- /**
- * 获取本月第一天和本月最后一天
- * [yyyy-MM-dd,yyyy-MM-dd]
- */
- export function defaultDate3() {
- const date = new Date();
- const startDate = new Date(date.getFullYear(), date.getMonth(), 1);
- const endDate = new Date(date.getFullYear(), date.getMonth()+1, 0);
- return [
- dateFormat(startDate, "yyyy-MM-dd"),
- dateFormat(endDate, "yyyy-MM-dd")
- ];
- }
- // 获得当前日期
- export function getCurrentDate(type = 'dateTime') {
- const date = new Date();
- const currentDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
- if (type == 'date') {
- return dateFormat(currentDate, 'yyyy-MM-dd')
- } else if (type == 'dateTime') {
- return dateFormat(date, 'yyyy-MM-dd hh:mm:ss')
- }
- }
- export function getYearDate(){
- const date = new Date();
- return date.getFullYear()
- }
|