如何在javascript中检查日期是否在本周内?

如何在javascript中检查日期是否在本周内?,javascript,angularjs,date,datetime,Javascript,Angularjs,Date,Datetime,我有这个日期“2016-04-23T11:45:00Z”,我想在本周检查一下这个日期,是不是 谢谢 <div ng-app="myApp"> <div class="container" ng-controller="Ctrl_List"> <h1>{{currentDate}}</h1> <h1>{{numberCurrentDateWeeks}}</h1> <h1>{{yourDa

我有这个日期“2016-04-23T11:45:00Z”,我想在本周检查一下这个日期,是不是

谢谢

<div ng-app="myApp">
<div class="container" ng-controller="Ctrl_List">

    <h1>{{currentDate}}</h1>
    <h1>{{numberCurrentDateWeeks}}</h1>

    <h1>{{yourDate}}</h1>
    <h1>{{numberYourDateWeeks}}</h1>

 </div>
</div>
然后你得到了一周的数据,只是比较或者做你喜欢做的任何事情


干杯

日期很难,我总是建议使用一个专门用于日期处理的库,因为它可以减少代码中出现错误的机会

这是个好主意

var now = moment();
var input = moment("2016-04-17T11:45:00Z");
var isThisWeek = (now.isoWeek() == input.isoWeek())

此链接解释了如何在不使用任何js库的情况下执行此操作

防止链接死亡的代码:

function( d ) { 

  // Create a copy of this date object  
  var target  = new Date(d.valueOf());  

  // ISO week date weeks start on monday  
  // so correct the day number  
  var dayNr   = (d.getDay() + 6) % 7;  

  // Set the target to the thursday of this week so the  
  // target date is in the right year  
  target.setDate(target.getDate() - dayNr + 3);  

  // ISO 8601 states that week 1 is the week  
  // with january 4th in it  
   var jan4    = new Date(target.getFullYear(), 0, 4);  

  // Number of days between target date and january 4th  
  var dayDiff = (target - jan4) / 86400000;    

  // Calculate week number: Week 1 (january 4th) plus the    
  // number of weeks between target date and january 4th    
  var weekNr = 1 + Math.ceil(dayDiff / 7);    

  return weekNr;    

}

可能不是最理想的解决方案,但我认为它非常可读:

function isThisWeek (date) {
  const now = new Date();

  const weekDay = (now.getDay() + 6) % 7; // Make sure Sunday is 6, not 0
  const monthDay = now.getDate();
  const mondayThisWeek = monthDay - weekDay;

  const startOfThisWeek = new Date(+now);
  startOfThisWeek.setDate(mondayThisWeek);
  startOfThisWeek.setHours(0, 0, 0, 0);

  const startOfNextWeek = new Date(+startOfThisWeek);
  startOfNextWeek.setDate(mondayThisWeek + 7);

  return date >= startOfThisWeek && date < startOfNextWeek;
}
函数为本周(日期){
const now=新日期();
const weekDay=(now.getDay()+6)%7;//确保星期日是6,而不是0
const monthDay=now.getDate();
const mondayThisWeek=月日-工作日;
const startOfThisWeek=新日期(+现在);
开始本周。设置日期(周一本周);
本周开始。设置小时数(0,0,0,0);
const startOfNextWeek=新日期(+startOfThisWeek);
开始下一周的设置日期(周一本周+7);
返回日期>=开始本周和日期<开始下一周;
}

通过检查
日期.getTime()
(自历元起的毫秒数)是否在上周一和下周一之间,您可以在没有任何库的情况下执行此操作:

const WEEK_LENGTH = 604800000;

function onCurrentWeek(date) {

    var lastMonday = new Date(); // Creating new date object for today
    lastMonday.setDate(lastMonday.getDate() - (lastMonday.getDay()-1)); // Setting date to last monday
    lastMonday.setHours(0,0,0,0); // Setting Hour to 00:00:00:00
    


    const res = lastMonday.getTime() <= date.getTime() &&
                date.getTime() < ( lastMonday.getTime() + WEEK_LENGTH);
    return res; // true / false
}
const周长=604800000;
函数onCurrentWeek(日期){
var lastmanday=new Date();//为今天创建新的日期对象
lastMonday.setDate(lastMonday.getDate()-(lastMonday.getDay()-1));//将日期设置为上周一
lastMonday.setHours(0,0,0,0);//将小时设置为00:00:00:00

const res=lastmanday.getTime()这似乎适合我

function isDateInThisWeek(date) {
  const todayObj = new Date();
  const todayDate = todayObj.getDate();
  const todayDay = todayObj.getDay();

  // get first date of week
  const firstDayOfWeek = new Date(todayObj.setDate(todayDate - todayDay));

  // get last date of week
  const lastDayOfWeek = new Date(firstDayOfWeek);
  lastDayOfWeek.setDate(lastDayOfWeek.getDate() + 6);

  // if date is equal or within the first and last dates of the week
  return date >= firstDayOfWeek && date <= lastDayOfWeek;
}

const date = new Date();
const isInWeek = isDateInThisWeek(date);
本周(日期)内的函数IsDate{
const todayObj=新日期();
const todayDate=todayObj.getDate();
const todayDay=todayObj.getDay();
//获取一周的第一个日期
const firstDayOfWeek=新日期(todayObj.setDate(todayDate-todayDay));
//获取一周的最后日期
const lastDayOfWeek=新日期(firstDayOfWeek);
lastDayOfWeek.setDate(lastDayOfWeek.getDate()+6);
//如果日期等于或在一周的第一个和最后一个日期内

return date>=firstDayOfWeek&&date如何知道4月22日星期五上午12点是本周的一个日期?你能回答吗?你有没有尝试过用周来代替完整日期?@Bálint,没有,我没有。你的一周是从星期一开始的,还是从星期天开始的?请注意,你的角度版本必须是1.3及以上才能让周数过滤器工作;)~示例:如果(s.numberCurrentDateWeeks==s.numberYourDateWeeks){//bingo它在同一个当前周内}如果(s.numberCurrentDateWeeks>s.numberYourDateWeeks){//它在过去的几周内}~“lastMonday.getTime()function isDateInThisWeek(date) { const todayObj = new Date(); const todayDate = todayObj.getDate(); const todayDay = todayObj.getDay(); // get first date of week const firstDayOfWeek = new Date(todayObj.setDate(todayDate - todayDay)); // get last date of week const lastDayOfWeek = new Date(firstDayOfWeek); lastDayOfWeek.setDate(lastDayOfWeek.getDate() + 6); // if date is equal or within the first and last dates of the week return date >= firstDayOfWeek && date <= lastDayOfWeek; } const date = new Date(); const isInWeek = isDateInThisWeek(date);