determining the quantity of dates

Is there a way to calculate the number of a specific day of the week occurring in a month using AngularJS? For example, how can I determine the count of Saturdays in a given month? Thank you. Here is the code snippet I have been working on:

<!doctype html>
<html ng-app="myApp">
<head>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
</head>
<body>

<div ng-app="myApp" ng-controller="myCtrl" ng-init="getdates()">
   <p ng-if="x='Sat'">{{dates.length}}</p>
   <div ng-repeat="x in dates">
    <input type="checkbox" ><p ng-show="x">{{x | format:'ddd'}}</p></input>
  </div>

</div>
  <script>



angular.module('myApp', [])
.controller('myCtrl', function($scope) {
  $scope.dates=[];
  $scope.getdates=function(){
    for(i=0;i<31;i++){
      $scope.myDate = i+' Feb 2015 00:00:00 GMT';
      $scope.dates.push($scope.myDate)
    }
  };

})
.filter('format', function() {
  return function(input, format) {
    return moment(new Date(input)).format(format);
  };
})
;
  </script>

</body>
</html>

Answer №1

Here is a step-by-step guide:

GET/INITIALIZE:

  • Provide the date as an argument
  • Determine the year and month of the input (yearVar, monthVar)
  • Identify the last day of that particular month (last_day_of_month)

NEXT

  • Commence from the first day with

    var currDate = new Date(yearVar, monthVar, 1)

    Locate the first instance of the required day with if(currDate.getDay() == 6) (Sunday is represented by 6)

  • Remember this value, repeat until the day is less than the last day of the month

DATE API Information

Visit: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using jQuery and regex to only allow alphanumeric characters, excluding symbols and spaces

Seeking advice, I am using a jquery function called alphanumers var alphanumers = /^[a-zA-Z0-9- ]*$/; which currently does not allow all symbols. However, I now wish to disallow the space character as well. Any suggestions? ...

Utilizing Lodash efficiently with tree shaking

After experimenting with using lodash functions as a specific import versus as a destructured object, I noticed a significant difference in file size. When imported as shown below, the size is only 14.7KB. However, when I tried importing as a destructured ...

limitation in bootstrap ensures that only one collapse element is open at any

Hey, can someone help me fix this code? I'm trying to make it so that only one element opens at a time, but I can't seem to figure out how. Right now, if I click on two elements, they both open and the other one doesn't close. This is what ...

Can you please guide me on how I can implement a top AJAX menu like the one on StackOverflow

Have you seen the menu that appears when you first visit a website, with an 'x' on the right to close it? I believe this is done through AJAX - what specific terms should I look up to implement this feature? ...

Load texture using ImageUtils with callback in Canvas Renderer

Currently, I am utilizing three.js revision 53. While attempting to load a texture in Canvas Renderer (specifically on Win7) and incorporating a callback for the onLoad event, the texture fails to display. Surprisingly enough, removing the callback functi ...

Storing the background color in a JavaScript variable

I've been experimenting with creating a fade in and out effect for a background image on a website. I've also been attempting to capture the background color of a div and store it in a variable. Here's what I have tried: elem = document.ge ...

Restrict Vue Router access to specific pages only

Once a user has logged in for the first time, they are directed to the CreateProfile page where they can enter their profile information. I want to restrict access to this page so that if the user manually types the URL into their browser (e.g. www.myproje ...

Resize the main container to fit the floated elements

I've been working on constructing a family tree, and the last part of the functionality is proving to be quite challenging for me. My family tree consists of list elements that are all floated to the left. Currently, when the tree expands beyond the ...

Check out the latest enhancements to React and Flask Fullstack right from your web browser

Recently, I ventured into the world of React and Flask development by following a tutorial found at this link. After completing the example fullstack website project, I realized that my code mirrored exactly what was provided by the author on their Github ...

The image is failing to animate according to the PNG sequence function

Enhanced Functionality: Upon clicking the "Tap Here" image button, a function called "GameStart()" is triggered. This function ensures that the main image "Star" descends down the page from the top through an animated sequence using png logic. The propose ...

Handling the display of divs using two select elements

I've been pondering this problem for a while now, and I'm starting to believe that there may not be a solution. Essentially, what I am trying to achieve is the ability to select options from two dropdown menus which will then show or hide specifi ...

Issue with writing JSON data to a file in node.js

When I try to write JSON objects from the Twitter API to a file using the fs.appendFile method, all that gets written is "[object Object]". The JSON objects look fine when logged to the console, so I'm not sure why this is happening. For example, the ...

The issue with NGX-Bootstrap/Angular Pagination arises when attempting to adjust the maxSize input while the screen view (width) is being altered

Currently, I am utilizing the Pagination component from Valor Software (click here to access). I am interested in adjusting the maxSize input dynamically based on changes in screen width. For reference, please see this example: Click to view example. It ...

Reversing Changes to the Database in Node.js using Mongoose on Error

In my node.js server, I have a complex express route that interacts with the database using mongoose, making multiple modifications. I'm looking to introduce a mechanism that can revert all changes in case of any error. My initial thought was to incl ...

Implementing pagination and filtering in a single MERN controller

Is it possible to implement pagination and filtering in the same controller? Filter service:- const filterPosts = async (filterData, token) => { const config = { headers: { Authorization: `Bearer ${token}`, }, data: { ...

Tips for effectively handling unique properties of a React component (such as Redux integration and custom CSS) when sharing through NPM distribution

Question: How can custom Redux containers and CSS be effectively managed with NPM? It can be challenging to handle these custom files through traditional package distribution platforms like NPM, especially when they need to be edited in various projects. ...

Angular $watch function not reflecting changes in the user interface

My $watch function is updating the model, but the bound controls in the UI are not reflecting the changes. Here is a screenshot of the updated model. https://i.stack.imgur.com/0ISg5.png The UI code looks like this: <form method="post" action="" role= ...

Utilizing AngularJS ng-repeat for traversing nested JSON structures

The data structure in my JSON file appears as follows: $scope.peoples = [people:{ name: 'Mike ', age: 20 }, people:{ name: 'Peter S ', age: 22 }]; Note: I am unable to alter the JSON structure. Below is the issue with ...

What could be causing the misalignment between the desired output and the response from the AJAX request

Below is a basic JavaScript file I am working with: $.ajax({ url: "is_complete.php", type: "post", success: function (data) { if(data == 1) { } alert("ok") } }) The message "ok" will only be di ...

How can I efficiently distribute a function that is linked to my scope across multiple controllers?

I have implemented a function that I am attaching to my scope like this. This function is crucial for my HTML pages, so I am using it in multiple controllers. Since all my controllers are top-level controllers, I cannot place this function in a higher-up c ...