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

Tips for increasing the size of a parent div when a child div is displayed with a set width?

Is there a way to make the parent div automatically expand when the child div with a fixed width is displayed onclick? Goal: I want the child div to show up when I click the link, and at the same time, I need the parent div to expand or scale to fit the ...

Guide to integrating react-phone-number-input into material-ui TextField

Would it be possible for me to use a Material UI TextField component as the inputComponent prop for the PhoneInput component from react-phone-number-input? I am facing an issue where I am unable to apply the ref successfully. Even though I can see the Mat ...

Dealing with Errors When Working with Angular Promises

Currently, I am in the process of mastering promises within Angular. In my code snippet, I have two "GET" requests that I need to execute sequentially. Everything is functioning properly, but I'm unsure about how to handle errors in this scenario. If ...

Tips for retrieving a selected date from an HTML textbox labeled as "Date"

My goal was to find the differences between two dates by utilizing an HTML Date textbox. <input type="text" name="datein" id="datein" value="" class="inputtexbox datepicker" style="display: none" is-Date/> <input type="text" name="dateto" id=" ...

Is it possible for a relative div to automatically adjust its height when the height of the inner absolute div increases?

I am working with the following HTML code snippet: <div id="content"> <div id="leftcol"> <p>Lorem Ipsum</p> </div> </div> along with this CSS styling: #content { width: 780px; padding: 10px; position: relative; back ...

Can you outline the key distinctions between AngularJS and ReactJS?

Looking to create a website that will be converted into a mobile application, I realize that my expertise lies more in desktop and Android native development rather than web client side development. After some research, I have decided to utilize HTML5, CSS ...

"Utilizing a custom CSS style for a half heart rating system

I am currently working on implementing a rating system using the font-awesome fa-heart class. While I have successfully colored the full heart, I am facing difficulty in filling only the first half of the heart in red. Despite my research efforts, all solu ...

Enhancing URLs with jQuery/AJAX: A Comprehensive Guide to Adding Parameters

Whenever I try to use the get method on an HTML form, the submitted data automatically appears in the URL. You can see what I mean here. Interestingly, when attempting to achieve the same result with JQuery and AJAX using the get method, the data doesn&apo ...

Creating a website with a seamless image background that fills the entire page

! Is it possible to achieve the same effect using AngularJS? Below is the CSS code: .bg { background: url(holiday-car-rental.jpg) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-s ...

Problem with Google+ Button Alignment

I arranged my social media buttons in a row - Twitter, Facebook & Google+. The issue is that the Google+ button is not aligning properly. It appears to be spaced out about 3 times further than the Twitter and Facebook buttons. I attempted adjusting the p ...

What is the best way to deactivate a hyperlink of an <a> tag specifically for a particular child within an element?

Imagine you have a clickable container that leads to another page when clicked. However, there are elements within this container that you want to disable so that clicking on them does not activate the link. How can this be achieved? For instance, ...

Having difficulties accessing the properties of a dynamically created JSON object with ng-repeat functionality

Within an ng-repeat loop, I have implemented a radio button that assigns the entire person object to a scope variable as shown below: <li ng-repeat="person in people"> <label>{{person.name}} <input type="radio" ng-model="$parent.s ...

ng-scrollbars may encounter difficulties when used within ng-view

Issue: I am experimenting with utilizing ng-scrollbars, a wrapper for Malihu's jQuery Custom Scrollbar by Manos Malihutsakis within an angular environment. Error: When implementing ng-view, vertical scrolling malfunctions (horizontal scrolls fine). ...

Insert a blank row at the top of the grid in Wijmo for Angular 2

I am attempting to insert a new empty row at the start of the grid when an external button is clicked. The grid is displaying correctly. <wj-flex-grid #flex [itemsSource]="data" [isReadOnly]="true" [headersVisibility]="'Column' ...

Lower the placement of Glyphicons

I have recently implemented a custom font on my website. Unfortunately, this font is not standard and its alignment is off. As a result, when I use this font with Twitter Bootstrap glyphicon, they do not appear in the same line. Is there a way to adjust t ...

Navbar remains visible despite ng-hide directive

I am having issues hiding a navbar based on the user's login status. Despite changing the $scope.loggedIn variable, the navbar does not hide as expected. Why is this happening? View: <nav> <div ng-controller="mainCtrl"> <!-- Lo ...

Troubleshooting: Issues with the functionality of AngularJS Material dialogs

I am facing an issue with displaying an Array of items in a dialog. Despite not receiving any errors, the output is not as expected. $scope.showDialog = function (ev) { $mdDialog.alert({ controller: 'DialogController', ...

Successfully launched Laravel application, experiencing issues with all pages except for the homepage

I recently completed a website for a client and everything seems to be working fine locally. Unfortunately, when trying to access any links on the site, I am getting a 500 server error. The hosting is with 1and1, and I've heard mixed reviews about it ...

Having trouble with the uib datepicker popup, displaying only the top portion

I'm facing an issue where the uib-datepicker popup that I copied from a template is only showing partially. Can anyone help me with this? ...

tips for asynchronously loading cloud endpoints APIs

gapi.client.load('myapi1', 'v1', function() { gapi.client.load('myapi2', 'v1', function() { gapi.client.load('myapi3', 'v1', function() { $rootscope.$broadcast( ...