"Enhance your date calculations with Moment JS by effortlessly adding

Take a look at the MomentJS code I am currently using:

var date = moment($scope.dt);

for (var i = 0; i < parseInt($scope.numPagos); i++) {
    $scope.resultados.push({
        'numerodecuota' : i + 1,
        'fechas' : date.add(1, 'days').format("MM/DD/YYYY"),
        'pagos' : Math.round($scope.importeprestamo / $scope.numPagos + interes),
        'interes' : Math.round(interes),
        'capital' : $scope.importeprestamo / $scope.numPagos,
        'fechaunix' : date.add(1, 'days').unix()
    }); 

}// End for loop

Here is the outcome of the above code:

https://i.sstatic.net/Rm85W.png

The dates should be in this format:

  • 01/12/2017
  • 01/13/2017
  • 01/14/2017
  • 01/15/2017

And so forth.

Answer №1

Keep in mind that moments are changeable. Using any of the manipulation methods will alter the original moment.

http://momentjs.com/docs/

It seems like you are adding one day, and then adding another day for the fechaunix date. Instead, try simply setting fechaunix to date once it has already been added:

$scope.resultados.push({
     'numerodecuota' : i + 1,
     'fechas' : date.add(1, 'days').format("MM/DD/YYYY"),
     'pagos' : Math.round($scope.importeprestamo / $scope.numPagos + interes),
     'interes' : Math.round(interes),
     'capital' : $scope.importeprestamo / $scope.numPagos,
     'fechaunix' : date.unix()
}); 

Answer №2

It seems like the issue is that you are doubling the addition of one day in your code

Firstly, here:

'fechas': date.add(1, 'days').format("MM/DD/YYYY"),

And then again, here:

  'fechaunix': date.add(1, 'days').unix()

To resolve this, make sure to add only once as shown in the example below

var app = angular.module("app", []);
app.controller("ctrl", function($scope) {
  var date = moment();
  $scope.numPagos="5";
  $scope.resultados=[];

  for (var i = 0; i < parseInt($scope.numPagos); i++) {
    $scope.resultados.push({
      'numerodecuota': i + 1,
      'fechas': date.add(1, 'days').format("MM/DD/YYYY"),
      //'pagos': Math.round($scope.importeprestamo / $scope.numPagos + interes),
      //'interes': Math.round(interes),
      //'capital': $scope.importeprestamo / $scope.numPagos,
      'fechaunix': date.unix()
    });

  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  {{resultados}}
</div>

Answer №3

Have you tried using the "date.add(1, 'days')" method twice?

If not, give this a shot:

       var date = moment($scope.dt);

        for (var i = 0; i < parseInt($scope.numPagos); i++) {
            var current = date.add(1, 'days');


            $scope.resultados.push({
                'numerodecuota' : i + 1,
                'fechas' : current.format("MM/DD/YYYY"),
                'pagos' : Math.round($scope.importeprestamo / $scope.numPagos + interes),
                'interes' : Math.round(interes),
                'capital' : $scope.importeprestamo / $scope.numPagos,
                'fechaunix' : current.unix()
            }); 

        }// End for loop

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

Authentication for REST API using JavaScript in the web browser

Currently, I am experimenting with creating a stateless, REST-based API that I intend to use from multiple sources, one of which will be a single-page Javascript web application. The goal is to have a unified API for different clients, even those developed ...

What is the best method for passing a customized feature to $modal in angular-ui-bootstrap?

There are two instances in which I need to trigger a modal using this code snippet: $modal.open({ templateUrl: "components/prize-info/prize-info.html", windowClass: "prize-info", scope: $scope, controller: &q ...

External JavaScript files are also subject to the same origin policy

Suppose a website http://www.mysite.com contains an external JavaScript file added like this: <script src="http://www.yoursite.com/new.js"></script> Within the http://www.yoursite.com/new.js JavaScript file, there is an AJAX call to a script ...

Encountering a JSONP error in Angular 2

Trying to make a jsonp request in angular 2, I am encountering an issue where I receive a response upon clicking the link of the error message. However, I am struggling to display this response on the browser and instead am faced with the following error: ...

Is there a way to automatically launch a new tab upon arriving from another website?

I currently have a website that displays a table of elements, each with its own set of sub-elements. On another page, I can view the element along with multiple tabs for its corresponding sub-elements. The current setup is depicted as follows: <a clas ...

Problem with the setInterval function causing issues with fade animations showing and hiding

I am currently using jQuery 3.2.1 and attempting to implement a text slider on my landing page. The goal is to switch between a few elements, displaying only one at a time. After a set amount of time, the current element should fade out while the next one ...

When working within a module using node.js and three.js, you may encounter an error message stating that "document

Currently, I am utilizing an app to execute various functions that I created in another .js file, where these functions are defined to facilitate drawing objects on a canvas. app.get('/visualization.html', function(req, res) { //Setting ...

How can I identify the appropriate default option element in AngularJS?

Query: I have a scenario where I need to set the default option in an angular dropdown menu based on values from an array of Person objects. How can I achieve this when initially loading the UI? http://plnkr.co/edit/hvIimscowGvO6Hje35RB?p=preview <!DO ...

Issue with Jquery - Variable not getting updated on resize event, causing multiple saves

After creating this jQuery script to enable/disable a fixed position div based on user scrolling behavior, I encountered an issue where the functionality breaks upon window resizing. Despite my attempts to update the variables correctly after resizing, the ...

Error: Unable to modify the value of a protected property '0' in the object 'Array'

I am facing a challenging issue with implementing a Material UI slider in conjunction with Redux. Below is the code for the slider component: import { Slider } from '@material-ui/core' const RangeSlider = ({handleRange, range}) => { ...

Guide on duplicating text and line breaks in JavaScript

There is some text that looks like this text = 'line 1' + "\r\n"; text+= 'line 2' + "\r\n"; text+= 'line 3' + "\r\n"; I have developed a function to help facilitate copying it to the clipboard ...

Guide to converting numerous lines of numbers into an array using JavaScript

I have a large collection of numbers that I need to convert into an array: 142156 108763 77236 78186 110145 126414 115436 133275 132634 ...... 82606 Is there a way to assign all these numbers to a variable and then convert it into an array? I'm consi ...

Having difficulty retrieving text from textarea with jquery

I'm experiencing an issue with my jquery script where the top two buttons are showing 'undefined' instead of performing the same action as the buttons below them. Could someone please pinpoint where I went wrong? I want the two buttons at t ...

Using scope in ng-style to manipulate a portion of a CSS property value in Angular

I've been attempting to add a border using ng-style, but I'm struggling to figure out how to concatenate the value from the scope variable. None of the methods below seem to be working for me: <div ng-style="{'border-top' :'1p ...

Add in a paragraph with the text that was previously submitted and make sure to refresh

Currently, I am working on honing my skills in jQuery. One of the exercises requires me to add a p element to the DOM upon submission, replacing the existing text without deleting the p tag. Unfortunately, I have been struggling with figuring out the best ...

Subheaders that stay in place while scrolling through a table with a stationary header

My goal is to design a table with a fixed header that allows the body to scroll under the header. While this is a common design, I am facing the challenge of implementing sticky subheaders. These subheaders should scroll along with the table until they rea ...

What is the best way to consistently display a scroll bar at the bottom?

How can I ensure that the scroll bar is always at the bottom when loading pages? I need an immediate solution. The JavaScript code seems to be malfunctioning. Please assist me in resolving this issue. #student_name{ height:315px; } <div id ...

VisualMap in ECharts featuring multiple lines for each series

If you'd like to view my modified ECharts option code, it can be found at this URL: Alternatively, you can also access the code on codesandbox.io : https://codesandbox.io/s/apache-echarts-demo-forked-lxns3f?file=/index.js I am aiming to color each l ...

What could be causing my Vue component to not refresh?

Can anyone help me figure out why this component isn't re-rendering after changing the value? I'm attempting to create a dynamic filter similar to Amazon using only checkboxes. Here are the 4 components I have: App.vue, test-filter.vue, filtersIn ...

How to implement server-side rendering in Next.js 14 with GraphQL queries

I recently completed a Next.js project and configured Apollo Client. I integrated it with my components, as shown in my layout.tsx file: import { Inter } from "next/font/google"; import "./globals.css"; import ApolloProviderClient from ...