如何在javascript中查看本周的日期?

时间:2016-04-22 07:45:20

标签: javascript angularjs date datetime

我有这个日期“2016-04-23T11:45:00Z”,我想在本周查看这个日期吗?

谢谢,

6 个答案:

答案 0 :(得分:4)

日期很难,我总是建议使用专用于日期处理的库,因为它可以减少代码中出错的可能性。

MomentJS是个好人。

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

答案 1 :(得分:2)

<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>

...

angular.module('myApp', [])
.controller("Ctrl_List", ["$scope", "$filter", function(s, $filter) {
  s.yourDate = '2016-04-23T11:45:00Z'
  s.currentDate = new Date();

  s.numberCurrentDateWeeks = $filter('date')(s.currentDate, "w");
  s.numberYourDateWeeks = $filter('date')(s.yourDate, "w");

}]);

然后你得到周数比较或做你喜欢的任何事情

欢呼!

答案 2 :(得分:1)

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

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;
}

答案 3 :(得分:1)

您可以通过检查date.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
}

(一周中的毫秒数= 24 * 60 * 60 * 1000 * 7 = 604,800,000)

答案 4 :(得分:0)

此链接解释了如何在不使用任何js库的情况下执行此操作。 https://gist.github.com/dblock/1081513

反对链接死亡的代码:

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;    

}

答案 5 :(得分:0)

这似乎对我有用。

private suspend fun monadsComposition() {
    val program: IO<String> = IO.fx {
        val option1 = !IO.effect { maybeString("hello") }
        val option2 = !IO.effect { maybeString("world") }
        val right1 = !IO.effect { eitherString("Combining") }
        val right2 = !IO.effect { eitherString("Monads") }
        val mergeOption = Option.fx {
            val optionValue1 = !option1
            val optionValue2 = !option2
            val right = Either.fx<Int, String> {
                val rightValue1 = !right1
                val rightValue2 = !right2
                "$optionValue1 - $optionValue2 - $rightValue1 - $rightValue2"
            }
            right.getOrElse { "Program has no output" }
        }
        mergeOption.getOrElse { "Program has no output" }
    }
    println(program.unsafeRunSync())
}