时间方法--根据不同时间段展示

/**
 * 
 * 
 * @return string ---返回字符串
 */
export function getTimeGreeting() {
    const date=new Date()
    const hours = date.getHours();
    let greeting = '';
    if (hours >= 5 && hours < 12) {
      greeting = '早上好';
    } else if (hours >= 12 && hours < 18) {
      greeting = '下午好';
    } else {
      greeting = '晚上好';
    }
    return greeting;
  }
/**
 * 
 * @param time ---时间戳
 * @return string ---返回xxxx-xx-xx类型的日期
 */
export function timeFormatting(time: string | number | Date){//获取日期
  const date =new Date(time)
  const  year=date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const formateDate=`${year}-${month}-${day}`
  return formateDate
}
/**
 * 
 * @return ---一段时间[xx-xx,xx-xx....] 
 */

export function getMonthDays(startDateStr: string | number | Date, endDateStr: string | number | Date) {
  const startDate = startDateStr ? new Date(startDateStr) : new Date();
  const endDate = endDateStr ? new Date(endDateStr) : new Date();
  const xAxisData = [];
  const currentDate = new Date(startDate.getTime());
  while (currentDate <= endDate) {
    const formattedDate = currentDate.toLocaleDateString(undefined, { month: '2-digit', day: '2-digit' });
    xAxisData.push(formattedDate);
    currentDate.setDate(currentDate.getDate() + 1);
  }
  
  return xAxisData;
}

/**
 * 
 * @return ---一段时间[xxxx-xx-xxx.....] 
 */
export function getCurrentMonthDates(startDate: string | number | Date, endDate: string | number | Date) {
  const start = new Date(startDate);
  const end = new Date(endDate);
  const dates = [];
  
  while (start <= end) {
    const year = start.getFullYear();
    const month = start.getMonth() + 1;
    const day = start.getDate();
    const dateStr = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
    dates.push(dateStr);
    
    start.setDate(start.getDate() + 1);
  }
  
  return dates;
}

/**
 * 是一个element组件的时间选择快捷方式
 * @param shortcutType  --字符串
 * @return object --返回一个对象 {text:xxx,value:[xxx,xxx]}
 */
export function dateTimePeriod(shortcutType: string) {
  const now = new Date();
  const currentYear = now.getFullYear();
  const currentMonth = now.getMonth();
  const getDateWithOffset = (offset: number) => {
      const date = new Date(now.getTime() + offset * 24 * 60 * 60 * 1000);
      return new Date(date.getFullYear(), date.getMonth(), date.getDate());
  };
  const ranges = {
      Today: { text: "Today", value: [now, now] },
      Yesterday: {
          text: "Yesterday",
          value: [
              new Date(currentYear, currentMonth, now.getDate() - 1),
              new Date(currentYear, currentMonth, now.getDate() - 1),
          ],
      },
      "Last 7 Days": {
          text: "Last 7 Days",
          value: [getDateWithOffset(-7), getDateWithOffset(-1)],
      },
      "Last 30 Days": {
          text: "Last 30 Days",
          value: [getDateWithOffset(-30), getDateWithOffset(-1)],
      },
      "This Month": {
          text: "This Month",
          value: [new Date(currentYear, currentMonth, 1), now],
      },
      "Last Month": {
          text: "Last Month",
          value: [
              new Date(currentYear, currentMonth - 1, 1),
              new Date(currentYear, currentMonth, 0),
          ],
      },
      "This Quarter": {
          text: "This Quarter",
          value: [
              new Date(currentYear, Math.floor(currentMonth / 3) * 3, 1),
              now,
          ],
      },
      "Last Quarter": {
          text: "Last Quarter",
          value: [
              new Date(currentYear, Math.floor(currentMonth / 3) * 3 - 3, 1),
              new Date(currentYear, Math.floor(currentMonth / 3) * 3, 0),
          ],
      },
  };
  return ranges[shortcutType as keyof typeof ranges] || [now, now];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值