获取当前时间、获取当前月份的第一天和最后一天、获取当前年份的第一天和最后一天

1、获取当前日期

// 获取当前日期
  export const  today = ()=> {
    let today = new Date();  //中国标准时间 当前时间
    let nowTime = today.getTime(); //当前时间时间戳
    today.setTime(parseInt(nowTime));
    let oYear = today.getFullYear();
    let oMoth = (today.getMonth() + 1).toString();
    if (oMoth.length <= 1) oMoth = "0" + oMoth;
    let oDay = today.getDate().toString();
    if (oDay.length <= 1) oDay = "0" + oDay;
    // 星期
    let days = today.getDay();
    switch (days) {
      case 1:
        days = '星期一';
        break;
      case 2:
        days = '星期二';
        break;
      case 3:
        days = '星期三';
        break;
      case 4:
        days = '星期四';
        break;
      case 5:
        days = '星期五';
        break;
      case 6:
        days = '星期六';
        break;
      case 0:
        days = '星期日';
        break;
    }
    let h = today.getHours().toString();
    if ((h).length <= 1) h = "0" + h;
    // 判断上午下午
    // if(h<12){
    //   h = "上午" + h
    //   if ((h).length <= 1) h = "0" + h;
    // }else{
    //   h = "下午" + (h-12)
    //   if (((h-12)).length <= 1) h = "0" + h;
    // }
    let m = today.getMinutes().toString();
    if (m.length <= 1) m = "0" + m;
    let s = today.getSeconds().toString();
    if (s.length <= 1) s = "0" + s;

    // 2022年04月20日  星期三 
    // return oYear + "年" + oMoth + "月" + oDay + "日  " + days + "  ";
    // 2022年04月20日  星期三  10:38:13
    // return o1Year + "年" + oMoth + "月" + oDay + "日  " + days + "  " + h + ":" + m + ":" + s;
    // 2022-04-20  星期三  10:36:09
    // return oYear + "-" + oMoth + "-" + oDay + "  " + days + "  " + h + ":" + m + ":" + s;
    // 2022-04-20 10:36:09
    return oYear + "-" + oMoth + "-" + oDay + " " + h + ":" + m + ":" + s;   
  }

2、获取当前月的第一天 例如 2022-04-01 00:00:00

// 获取当前月的第一天 例如 2022-04-01 00:00:00
  export const currmonthStarttime = () => {
    let today = new Date();  //中国标准时间 当前时间
    // 设置一个月的某一天
    today.setDate(1)
    let oYear = today.getFullYear();
    let oMoth = (today.getMonth() + 1).toString();
    if (oMoth.length <= 1) oMoth = "0" + oMoth;
    let oDay = today.getDate().toString();
    if (oDay.length <= 1) oDay = "0" + oDay;

    return oYear + "-" + oMoth + "-" + oDay + " " + '00' + ":" + '00' + ":" + '00';
  }

3、获取当前年的第一天 例如 2022-01-01 00:00:00

// 获取当前年的第一天 例如 2022-01-01 00:00:00
  export const curryearStarttime = () => {
    var firstDay = new Date();
    firstDay.setDate(1);
    firstDay.setMonth(0);
    firstDay = firstDay.Format("yyyy-MM-dd");

    return firstDay + " " + "00:00:00"
  }

4、获取当前月份的第一天和最后一天

  // 获取当前月份的第一天和最后一天 
  // 例如 2019-09-01 00:00:00 ~ 2019-09-30 23:59:59
  export const getMonthFirstLastDay =()=> {
    var myDate = new Date();
    var currentMonth = myDate.getMonth();
    var firstDay = new Date(myDate.getFullYear(), currentMonth, 1)
    var lastDay = new Date(firstDay.getFullYear(), currentMonth + 1, 0);
    firstDay = firstDay.Format("yyyy-MM-dd");
    lastDay = lastDay.Format("yyyy-MM-dd");
    return (firstDay + " " + "00:00:00") + "~" + (lastDay + " " + "23:59:59");
  }

5、获取当前年份的第一天和最后一天

  // 获取当前年份的第一天和最后一天 
  // 例如 2019-01-01 00:00:00: ~ 2019-12-31 23:59:59
  export const getYearFirstLastDay =()=>{
    var firstDay = new Date();
    firstDay.setDate(1);
    firstDay.setMonth(0);
    var lastDay = new Date();
    lastDay.setFullYear(lastDay.getFullYear()+1);
    lastDay.setDate(0);
    lastDay.setMonth(-1);
    firstDay = firstDay.Format("yyyy-MM-dd");
    lastDay = lastDay.Format("yyyy-MM-dd");
    return (firstDay + " " + "00:00:00") + "~" + (lastDay + " " + "23:59:59");
  }
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐