Vue.js-各时间date处理函数封装

Date.js

export const MILLISECOND_PER_SECOND = 1000;
export const MILLISECOND_PER_MINUTE = 60 * MILLISECOND_PER_SECOND;
export const MILLISECOND_PER_HOUR = 60 * MILLISECOND_PER_MINUTE;
export const MILLISECOND_PER_DAY = 24 * MILLISECOND_PER_HOUR;
export const MILLISECOND_PER_MONTH = 30 * MILLISECOND_PER_DAY;
export const MILLISECOND_PER_YEAR = 365 * MILLISECOND_PER_MONTH;

function addZero(t) {
  let a = t;
  if (a < 10) {
    a = `0${a}`;
  }
  return a;
}

/**
 * @description: 输入Date或者时间戳,返回0时0分0秒的Date(非UTC)
 * @param {Date | milliseconds} 必选
 * @return: {Date} 返回0时0分0秒的Date
 */
export function clearTime(date) {
  const d = new Date(date);
  if (d.toString() === "Invalid Date") {
    return false;
  }
  d.setHours(0);
  d.setMinutes(0);
  d.setSeconds(0);
  d.setMilliseconds(0);
  return d;
}

/**
 * @description: 输入Date对象或者时间戳,返回 YYYY-MM (非UTC)格式的字符串,分隔符默认 - ,可自定义
 * @param {Date | milliseconds} 必选
 * @param {String} 可选,用于年月日之间的分隔符
 * @return: {String} 返回 YYYY-MM-DD 格式的字符串
 */
export function tranTimeYM(time, split = "-") {
  const d = new Date(time);
  if (d.toString() === "Invalid Date") {
    return false;
  }
  let month = d.getMonth() + 1;
  month = addZero(month);
  const timeYM = d.getFullYear() + split + month;
  return timeYM;
}

/**
 * @description: 输入Date对象或者时间戳,返回 YYYY-MM-DD (非UTC)格式的字符串,分隔符默认 - ,可自定义
 * @param {Date | milliseconds} 必选
 * @param {String} 可选,用于年月日之间的分隔符
 * @return: {String} 返回 YYYY-MM-DD 格式的字符串
 */
export function tranTimeYMD(time, split = "-") {
  const d = new Date(time);
  if (d.toString() === "Invalid Date") {
    return false;
  }
  let month = d.getMonth() + 1;
  let date = d.getDate();
  month = addZero(month);
  date = addZero(date);
  const timeYMD = d.getFullYear() + split + month + split + date;
  return timeYMD;
}

/**
 * @description: 输入Date对象或者时间戳,返回 HH:MM:SS (非UTC)格式的字符串,分隔符默认 : ,可自定义
 * @param {Date | milliseconds} 必选
 * @param {String} 可选,用于时分秒之间的分隔符
 * @return: {String} 返回 HH:MM:SS 格式的字符串
 */
export function tranTimeHMS(time, split = ":") {
  const d = new Date(time);
  if (d.toString() === "Invalid Date") {
    return false;
  }
  const hours = addZero(d.getHours());
  const minutes = addZero(d.getMinutes());
  const seconds = addZero(d.getSeconds());
  const timeHMS = hours + split + minutes + split + seconds;
  return timeHMS;
}

/**
 * @description: 输入Date对象或者时间戳,返回 YYYY-MM-DD HH:MM:SS (非UTC)格式的字符串,分隔符可自定义
 * @param {Date | milliseconds} 必选
 * @param {String} 可选,用于年月日之间的分隔符
 * @param {String} 可选,用于时分秒之间的分隔符
 * @return: {String} 返回 YYYY-MM-DD HH:MM:SS 格式的字符串
 */
export function tranTimeYMDHMS(time, split, split2) {
  const d = new Date(time);
  if (d.toString() === "Invalid Date") {
    return false;
  }
  return `${tranTimeYMD(time, split)}${tranTimeHMS(time, split2)}`;
}

/**
 * @description: 传入时间(毫秒),转化为对应的中文
 * @param {Number}  必选,输入毫秒
 * @return: {String} , 返回对应的中文时间
 */
export function getTimeUnit(timestamps) {
  if (typeof timestamps !== "number" || timestamps <= 0) {
    return "0秒";
  }
  if (timestamps >= MILLISECOND_PER_YEAR) {
    return `${parseInt(timestamps / MILLISECOND_PER_YEAR, 0)}`;
  }
  if (timestamps >= MILLISECOND_PER_MONTH) {
    return `${parseInt(timestamps / MILLISECOND_PER_MONTH, 0)}个月`;
  }
  if (timestamps >= MILLISECOND_PER_DAY) {
    return `${parseInt(timestamps / MILLISECOND_PER_DAY, 0)}`;
  }
  if (timestamps >= MILLISECOND_PER_HOUR) {
    return `${parseInt(timestamps / MILLISECOND_PER_HOUR, 0)}小时`;
  }
  if (timestamps >= MILLISECOND_PER_MINUTE) {
    return `${parseInt(timestamps / MILLISECOND_PER_MINUTE, 0)}分钟`;
  }
  if (timestamps >= MILLISECOND_PER_SECOND) {
    return `${parseInt(timestamps / MILLISECOND_PER_SECOND, 0)}`;
  }
  return false;
}

/**
 * @description: 将时间日期转化为时间戳(支持格式 2017-12-12 12:12:12)
 * @param {Date} 需要转化的时间
 * @return: {milliseconds} 返回对应时间的时间戳
 */
export function timeToStamps(time) {
  return new Date(time).getTime();
}

/**
 * @description: 将ecel表格中的日期转化为时间戳
 * @param {Date} 需要转化的时间
 * @return: {milliseconds} 返回对应时间
 */

export function timeToyear(daytime) {
  if (typeof daytime === "number") {
    const newTime = new Date().getTimezoneOffset() / 60;
    const time = new Date(((daytime - 1) * 24 + newTime) * 3600000 + 1);
    time.setYear(time.getFullYear() - 70);
    return tranTimeYMDHMS(time, "-", ":");
  }
  return daytime;
}

/**
 * @description: 一个月
 * @return: {milliseconds} 返回对应时间
 */

export function getPreMonth() {
  let now = new Date();
  const year = now.getFullYear(); // getYear()+1900=getFullYear()
  let month = now.getMonth() + 1; // 0-11表示1-12月
  let day = now.getDate();
  if (parseInt(month, 0) < 10) {
    month = `0${month}`;
  }
  if (parseInt(day, 0) < 10) {
    day = `0${day}`;
  }
  now = `${year}-${month}-${day}`;
  if (parseInt(month, 0) === 1) {
    // 如果是1月份,则取上一年的12月份
    return `${parseInt(year, 0) - 1}-12-${day}`;
  }
  const preSize = new Date(year, parseInt(month, 0) - 1, 0).getDate(); // 上月总天数
  if (preSize < parseInt(day, 0)) {
    // 上月总天数<本月日期,比如3月的30日,在2月中没有30
    return `${year}-${month}-01`;
  }
  if (parseInt(month, 0) <= 10) {
    return `${year}-0${parseInt(month, 0) - 1}-${day}`;
  }
  return `${year}-${parseInt(month, 0) - 1}-${day}`;
}

/**
 * @description: 上一个月
 * @return: {milliseconds} 返回对应时间
 */
export function getTwoPreMonth() {
  let now = new Date(getPreMonth());
  const year = now.getFullYear(); // getYear()+1900=getFullYear()
  let month = now.getMonth() + 1; // 0-11表示1-12月
  let day = now.getDate();
  if (parseInt(month, 0) < 10) {
    month = `0${month}`;
  }
  if (parseInt(day, 0) < 10) {
    day = `0${day}`;
  }
  now = `${year}-${month}-${day}`;
  if (parseInt(month, 0) === 1) {
    // 如果是1月份,则取上一年的12月份
    return `${parseInt(year, 0) - 1}-12-${day}`;
  }
  const preSize = new Date(year, parseInt(month, 0) - 1, 0).getDate(); // 上月总天数
  if (preSize < parseInt(day, 0)) {
    // 上月总天数<本月日期,比如3月的30日,在2月中没有30
    return `${year}-${month}-01`;
  }
  if (parseInt(month, 0) <= 10) {
    return `${year}-0${parseInt(month, 0) - 1}-${day}`;
  }
  return `${year}-${parseInt(month, 0) - 1}-${day}`;
}

/**
 * @description: 请求所需时间time
 * @param {time} 天数
 * @return: {milliseconds} 返回对应时间
 */

export function getPreDay(time, days) {
  const now = new Date(time);
  const sdtime3 = now.setDate(now.getDate() - days); // 往前day天
  return sdtime3;
}
/**
 * @description: 请求所需时间time
 * @param {time} 天数
 * @return: {milliseconds} 返回对应时间
 */

export function getPreMon(time, mon) {
  const now = new Date(time);
  const sdtime3 = now.setMonth(now.getMonth() - mon); // 往前月
  return sdtime3;
}

/**
 * @description: 计划开始时间
 * @param 每隔几天,0-代表每天,1-代表日期间隔一天
 * @return: {milliseconds} 返回对应时间
 */
// startDate: 开始时间;
// dayLength:每隔几天获取一天
export function getTargetDate(date, daylength) {
  let dayLength = daylength;
  dayLength += 1;
  const tempDate = new Date(date);
  tempDate.setDate(tempDate.getDate() + dayLength);
  const year = tempDate.getFullYear();
  const month =
    tempDate.getMonth() + 1 < 10
      ? `0${tempDate.getMonth() + 1}`
      : tempDate.getMonth() + 1;
  const day =
    tempDate.getDate() < 10 ? `0${tempDate.getDate()}` : tempDate.getDate();
  return `${year}-${month}-${day}`;
}

export function getDateStr(startDate, endDate, dayLength) {
  const str = [];
  let startdate = startDate;
  str.push(startDate);
  for (let i = 0; ; i + 1) {
    const getDate = getTargetDate(startdate, dayLength);
    startdate = getDate;
    if (getDate <= endDate) {
      str.push(getDate);
    } else {
      break;
    }
  }
  return str;
}

/**
 * @description: 计划时间区间内的所有月份 , 传入格式YYYY-MM
 * @param 开始时间, 结束时间
 * @return: {milliseconds} 返回对应月份数 Array
 */
export function getMonthBetween(start, end) {
  const result = [];
  const s = start.split("-");
  const e = end.split("-");
  const min = new Date();
  const max = new Date();
  min.setFullYear(s[0], s[1] * 1 - 1, 1); // 开始日期
  max.setFullYear(e[0], e[1] * 1 - 1, 1); // 结束日期
  const curr = min;

  // eslint-disable-next-line no-unmodified-loop-condition
  while (curr <= max) {
    const month = curr.getMonth();
    // console.log(month + 1)
    // var str = curr.getFullYear() + "-" + (month);
    // var s = curr.getFullYear() + "-0";
    // if (str == s) {
    // str = curr.getFullYear() + "-1";
    // }
    result.push(`${month + 1}`);
    curr.setMonth(month + 1);
  }
  // result.splice(0, 1)
  return result;
}

/**
 * [isDuringDate 比较当前时间是否在指定时间段内] 传入格式YYYY-MM-DD HH:mm:ss
 * @DateTime 2020-03-30
 * @version 1.0
 * @param  date  [beginDateStr] [开始时间]
 * @param  date  [endDateStr]  [结束时间]
 * @param  date  [DateTime]  [目标时间]
 * @return  Boolean
 */

export function isDuringDate(beginDateStr, endDateStr, DateTime) {
  const curDate = new Date(DateTime);
  const beginDate = new Date(beginDateStr);
  const endDate = new Date(endDateStr);
  if (curDate >= beginDate && curDate <= endDate) {
    return true;
  }
  return false;
}

Template使用:

import { tranTimeYMDHMS } @/components/Date.js;  // 示例:引入返回 年-月-日-时-分-秒

tranTimeYMDHMS(new Date()); // console.log : xx-xx-xx-xx-xx-xx 年-月-日-时-分-秒

你可能感兴趣的:(Vue.js,js,javascript,vue.js)