获取当前时间、获取当前月的第一天、获取当前年的第一天

本文介绍了JavaScript中获取当前日期、月份第一天和最后一天以及年份第一天和最后一天的方法。提供了相关函数实现,包括获取当前日期时间、当前月的第一天、当前年的第一天以及当前月和年的起止日期。这些函数对于处理时间范围的计算非常有用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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");
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值