Adding a certain number of days to a date within an existing ng-module in AngularJS

Hey everyone, I'm new to using AngularJS and I am currently attempting to incorporate one ng-module value into another ng-module date input within AngularJS. For example: if the date is 17-08-2016 and we add 20 days, the expected result should be 03-09-2016. Here is my plunk for reference.

I am struggling to identify where I went wrong and how to find a solution. If anyone here has insight or knows the answer, I would greatly appreciate your help. Thank you.

This is My Controller:

 $scope.name = {
"_id": "57ac1b6d82e1c5940ac3c730",
"user": {
"_id": "57400c32bd07906c1308e2cf",
"displayName": "mani selvam"
},
"__v": 0,
"terms": "20",
"invoice_date": "2016-08-17",
"due_date": ""
};

Date.prototype.addDays = function(days) {
    this.setDate(this.getDate() + parseInt(terms));
    return this;
};

This is My HTML:

 <tr ng-repeat="mani in name"> </tr>
      <td >{{name.invoice_date | date:'dd-MM-yyyy'}}</td>
          <td >{{name.terms }}</td>
             <td >{{name.invoice_date | date:'dd-MM-yyyy'}} + {{name.terms }}</td>

Answer №1

If you encounter an issue, follow these steps to resolve it:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = {
"_id": "57ac1b6d82e1c5940ac3c730",
"user": {
"_id": "57400c32bd07906c1308e2cf",
"displayName": "mani selvam"
},
"__v": 0,
"terms": "20",
"invoice_date": "2016-08-17",
"due_date": ""
};

$scope.addDays = function(stringDate,days) {
   var date = new Date(stringDate);
   date.setDate(date.getDate() + parseInt(days));
  return date;
}

});

Next, include the following in your template:

 <td >{{addDays(name.invoice_date,name.terms) | date:'dd-MM-yyyy'}}</td>

The reason why a similar method may not work in your scenario:

<td >{{(name.invoice_date | date:'dd-MM-yyyy').addDays({{name.terms}})}} + {{name.terms }}</td>

This is because the angular date filter produces a String output and is not directly linked to the date prototype

In theory, if your name.invoice_date was already in date object format rather than being a string in your json data, this could potentially work. Refer to this excerpt from the angular filter source code

@param {(Date|number|string)} date Date to format either as Date object, milliseconds (string or
 *    number) or various ISO 8601 datetime string formats (e.g. yyyy-MM-ddTHH:mm:ss.sssZ and its
 *    shorter versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). If no timezone is
 *    specified in the string input, the time is considered to be in the local timezone.

Therefore, if it was originally a date object, you could use:

<td >{{(name.invoice_date).addDays({{name.terms}}) | date:'dd-MM-yyyy'}} + {{name.terms }}</td>

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

What steps should I take to fix the error occurring in my MEAN project?

I'm encountering an issue while trying to run grunt "Cannot find module './api" The error points to the 'routes.js' file --->click here for a visual representation of the project's structure. app.use('/api/things', ...

What could be causing my ng-repeat directive to trigger an HTTP request error?

Using Anguar UI-Router, the state 'seeFoos' is set up with Angular: angular.module('foos').config(['$stateProvider', function($stateProvider) { $stateProvider. state('seeFoos', { url: '/foos&apo ...

display a different selection option depending on the previously chosen item

I'm working on displaying additional options in a select box when the user makes a selection from the main select box. I've set display:none for the select boxes, which will only appear when the user chooses an option from the main select box. C ...

Prevent selection of specific weekdays in Kendo grid calendar

When editing a record in a Kendo grid with a date field, is there a way to only allow the selection of Monday and disable all other days of the week? ...

Enhancing User Interaction with Bootstrap Input Forms

Struggling with a CSS and Bootstrap issue, I am unable to find a solution. The problem I am facing is that the content in a div form is not responsive within its parent div. It always extends beyond the parent div and does not adjust its size when the scre ...

"Want to make a phone call with an extension? Just click here

The issue at hand is rather straightforward. Is there a way to create a link for mobile browsers that would automatically dial a phone number and input an extension without requiring any action from the user? I am familiar with using tel:, as well as spe ...

Guide on transferring an HTML template to a React component using props

I've created a React popup window component that accepts templates via props. Check out this example of how the popup is used: popup = ( <div> <Popup text='This is a popup' popupEl={ popupEl } /> < ...

numbered navigation jquery plugin

Can anyone recommend a jQuery plugin or alternative solution for creating a navigation system for comments similar to YouTube? Here is the link for reference: Thank you in advance. ...

Combining pdfmake with AngularJS and Electron results in the generation of a new PDF document

I am having an issue with integrating the pdfmake library into my AngularJS and Electron project. The PDF generated appears blank. Here is the code snippet: .service('PDFService', function() { this.createPdfOne = function(data) { ...

Error: Authorization required to access server-side resource [POST http://localhost:3000/serverSide] - Status

I'm having an issue with sending a username and password from an HTML file to a Javascript file that queries an employee table for authentication. The problem arises when the username and password are set to undefined in my serverSide.js file, prevent ...

Using jQuery-Ajax to fetch MySQL data in Perl

Currently, I am in the process of developing a webpage that consists of three essential components: a select box, a button, and a table. At a high level, the user makes a selection from the element and then clicks the button. Subsequently, a PERL script is ...

What is the best way to ensure that the larger child divs always fit perfectly within the parent div?

Creating a Responsive Layout <div class="container"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> CSS Styling .box1, .box2, .box3{ display: block; f ...

An element featuring a background color is vertically aligned in the middle of its parent container

Struggling to achieve a seemingly simple task, but coming up short on finding a solution. The goal is to have a background-color that aligns vertically in the middle of the first and last images in a stack of images. It may sound more complicated than it a ...

Can you explain how to utilize theme values in CSS within Mui?

Working on my React app with mui themes, I have the following in index.js: let theme = createTheme({ palette: { primary: { main: "#00aaa0", contrastText: '#fcf4d9', }, secondary: { main: "#D55B3E&quo ...

SweetAlert.js does not function properly within a custom AngularJS directive

When creating a list to display in a modal, I utilized directives for the modal implementation. (I actually didn't write these directives myself) Within the list, each item features two buttons - one for editing the name and the other for deleting t ...

Tips for retrieving the distance from the top of a specific div element and transferring it to another div

How can I add margin-top based on the tab that is clicked? Specifically, when TAB 4 is selected, I want the content to remain in the same position from the top. https://i.sstatic.net/ObDUg.jpg ...

What is the best way to implement ng-model in conjunction with ng-repeat?

I created a form using ng-repeat, but I am struggling to incorporate ng-model into it. I attempted to use track by $index without success. While I found examples of using ng-model with values in an object, that's not what I'm looking for. I simp ...

"Customizing the topbar of Twitter Bootstrap for right-to

Attempting to implement twitter bootstrap for a top navigation bar on my master page. http://jsfiddle.net/ZqCah/1/ Encountering some issues and seeking assistance: 1- Desiring to switch the direction of all content to right-to-left (rtl). This would me ...

The Bootstrap column class is malfunctioning

Recently, I've been diving into the world of web development. I attempted to create three divs in one row using Bootstrap col class, but unfortunately it didn't turn out as expected. Two divs ended up in a row with some blank space, while the thi ...

How can I retrieve both the keys and values of $scope in AngularJS?

Here is the code I have written to retrieve all key values using $scope. $scope.Signup={}; $scope.Message=''; $scope.SubmitPhysicianSignup = function() { var url = site_url + 'webservices/physician_signup'; //console.lo ...