获取本周本月本季本年第一天,获取今天星期几、当前日期的前一天

获取上周第一天和最后一天

const now = new Date();
const nowTime = now.getTime();
const second = nowTime % 86400000;
const day = (now.getDay() + 6) % 7;
const endTime = nowTime - second + new Date().getTimezoneOffset() * 60000 - day * 86400000;
//第一天
const lastWeekFirstDay = new Date(endTime - 86400000 * 7);
//最后一天
const lastWeekLastDay = new Date(endTime - 1000);

获取本周第一天和最后一天

//第一天:方法1
getFirstDayOfWeek() {
    let nowTime = new Date();
    let aa = nowTime.setDate(nowTime.getDate() - nowTime.getDay() + 1);
    let week = nowTime.getFullYear() + "-" + (nowTime.getMonth() + 1) + "-" + nowTime.getDate();
    return week;
  },
  
//第一天:方法2
 const start = new Date(new Date().getTime() - 3600 * 1000 * 24 * (new Date().getDay() - 1));
 const currentWeekFirstDay= moment(start).format("YYYY-MM-DD");
 
//最后一天
import moment from "moment";

const currentWeekLastDay= moment(end).format("YYYY-MM-DD")";

获取下周第一天和最后一天

 const now = new Date();
const nowTime = now.getTime();
const second = nowTime % 86400000;
const day = (now.getDay() + 6) % 7;
const endTime = nowTime - second + new Date().getTimezoneOffset() * 60000 - day * 86400000;
//第一天
const nextWeekFirstDaty = new Date(endTime + 86400000 * 7);
//最后一天
const nextWeekLastDay = new Date(endTime  + 86400000 * 13);

获取上月第一天和最后一天

  const now = new Date();
  const nowTime = now.getTime();
  const second = nowTime % 86400000;
  const date = now.getDate();
  const endTime = nowTime - second + new Date().getTimezoneOffset() * 60000 - date * 86400000 + 86400000;
  const lastDate = new Date(endTime - 1000).getDate();
  //第一天
  const lastMonthFirstDay = new Date(endTime - lastDate * 86400000)
  //最后一天
  const lastMonthLastDay = new Date(endTime - 1000);

获取本月第一天和最后一天

//第一天
let currentMonthFirstday = new Date().setDate(1);
//最后一天
const date = new Date();
const currentMonthLastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);

获取下月第一天和最后一天

  const inDate = new Date();
  const year = inDate.getFullYear();
  const month = inDate.getMonth() + 1;
  //第一天
  const nextMonthFirstDay = new Date(year, month, 1).toLocaleDateString();
  //最后一天
  const nextMonthLastDay= new Date(year, month + 1, 0).toLocaleDateString();;

获取本季度的第一天

function getSeasonFirstday() {
   var now = new Date();
   var month = now.getMonth();
   if (month < 3) {
     now.setMonth(0);
   } else if (2 < month && month < 6) {
     now.setMonth(3);
   } else if (5 < month && month < 9) {
     now.setMonth(6);
   } else if (8 < month && month < 11) {
     now.setMonth(9);
   }
   now.setDate(1);
   return now;
 }

获取今年第一天

let year = new Date().getFullYear();
let yearFirstday = new Date(year, 0, 1);

获取当天是星期几

function getWeekDate() {
  var now = new Date();
  var day = now.getDay();
  var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
  var week = weeks[day];
  return week;
}

获取昨天的日期

function getYestToday() {
  return new Date().getTime() - 24 * 60 * 60 * 1000;
}

获取明天的日期

const nowTime = new Date().getTime();
const second = nowTime % 86400000;
const tomorrow= new Date(nowTime - second + 86400000 + new Date().getTimezoneOffset() * 60000);

获取最近一个月的日期

function getLatesDate() {
  const end = new Date();
  const start = new Date();
  start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
  return [start, end];
};

获取最近一年的月份

function getLatesDate() {
  const end = new Date();
  const start = new Date();
  start.setTime(start.getTime() - 3600 * 1000 * 24 * 30 *12);
  return [start, end];
};

获取最近5年的年份

function getLatesDate() {
  const end = new Date();
  const start = new Date();
  start.setTime(start.getTime() - 3600 * 1000 * 24 * 30 * 12 * 5);
  return [start, end];
};
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值