The current date is cycling back to the month before

There is a datetime received from my api as 2018-09-01T00:00:00.000Z, referred to as frame.scandate.

Another date is generated within the program as 2018-09, simply known as scandate. These examples can represent any year/month combination.

In my code:

    this.allStations.forEach(station => {
        station.frames.forEach(frame => {
            if(moment(frame.scandate).isSame(moment(scandate), 'month')){
                total+=frame.framesTotal;
            }
        })

This compares the previous frame.scandate with the current scandate.

For instance:

scandate = '2018-09';
frame.scandate = '2018-09-01T00:00:00.000Z';
console.log(moment(scandate).format('YYYY-MM'));
console.log(moment(frame.scandate).format('YYYY-MM'));

The output will be:

2018-09
2018-08

By modifying the code in this way, the issue was resolved:

    this.allStations.forEach(station => {
        station.frames.forEach(frame => {
            if(moment(frame.scandate).add(1, 'minute').isSame(moment(scandate), 'month')){
                total+=frame.framesTotal;
            }
        })

The key change being .add(1, 'minute').

Is this discrepancy due to the time value of 00:00:00Z in the frame.scandate? Any insight on this would be appreciated.

Answer №1

It seems like the issue may be related to differences in timezones.

When running this specific script in Spain

var moment = require('moment'); // This code was tested in a nodejs environment
var scandate = '2018-09';
var result = moment(scandate);
console.log(moment(result).format('YYYY-MM-DD'))

The output is 2018-09-01


To resolve this, one solution is to initialize frame.scandate in the following way:

frame.scandate = moment.utc('2018-09-01T00:00:00.000Z');

By using moment.utc() instead of just moment(), the expected output is achieved.

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

Implementing debounceTime in Angular can be done by using the debounceTime operator

I am currently working on implementing the debounceTime functionality in an autocomplete feature. My goal is to use debounceTime to minimize the number of server calls. After researching, I found 3 possible solutions which I have included snippets of belo ...

Having trouble with the Ng multiselect dropdown displaying empty options?

I'm currently facing a challenge in adding a multiselect dropdown feature to my project. Below is the code I have been working on: HTML <ng-multiselect-dropdown [settings]="searchSettings" [data]="dummyList" multiple> </n ...

What is the best way to terminate a MongoDB client connection in the event of an error?

I am currently managing a configuration where I have set up a MongoDB instance and operating two JavaScript services on a Linux server. One of the services, moscaService.js, is responsible for listening to MQTT topics on the server and storing the incoming ...

I need to link the click function to the li components within the xpages autocomplete feature

I've successfully implemented Tim Tripcony's advanced type-ahead solution and it's working well. However, I'd like to customize it a bit by redirecting the user to the selected document instead of filling the editbox with the selected d ...

What is the process of assigning data, in JSON format, from an HTML form to a variable?

I have the coding below in my abc.html file, which converts form data to JSON format: <body> <form enctype='application/json' method="POST" name="myForm"> <p><label>Company:</label> <input name=& ...

Deriving types from object combinations

Can the Foo type be 'flattened' to return { A?: string; B? number } in the code snippet below? type Foo = { A: string } | { B: number } type Flatten< T, Keys extends keyof T = T extends any ? keyof T : never, > = { [K in Keys]?: T[K] } ...

Tips for handling user click events in Angular 2?

In Angular2, I am facing an issue with two components. When a user clicks a button in component1, a method is triggered that stores data in the shared service to a variable. However, component2's ngOnInit() method initializes this variable to undefine ...

Validate with JavaScript, then display and submit

I've created a submit function to verify form inputs, and then, if desired (via checkbox), print as part of the submission process. The issue is that when printing, the form submission never finishes. However, without printing, the form submits corre ...

Notify other components in Angular when a change has occurred without relying on intervals

In the footer component of my project, I currently have a code snippet that runs a check on LocalStorage every 15 seconds using a timer. ngOnInit() { const checkLocalStorage = interval(15000); checkLocalStorage.subscribe(data => { ...

Encountering an error stating that 'coordinates should consist of an array with two or more positions'

Utilizing turf.js to generate a line depicting the path of an individual while their location is tracked. An array of coordinate arrays resembling Turf.js (lineString) is causing this error: Uncaught Error: coordinates must be an array of two or more posi ...

Learning about a basic sorting algorithm that analyzes and compares different values

While on the hunt for a simple jQuery sorting script, I stumbled upon this gem online: $(function() { $('ol').each(function() { var matches = $('li', this).filter(function() { // Each item var text = $(this).te ...

The imported JS file shows a warning that the variable 'myProperty' is defined but not utilized anywhere

When I try to import a variable from the same folder, why am I getting an error message saying it is defined but not used? I am sure that I am using it. Your input would be greatly appreciated! error 'componentName' is defined but never used. ...

Transmit the Selected Options from the Checkbox Categories

Here's an intriguing situation for you. I've got a webpage that dynamically generates groups of checkboxes, and their names are unknown until they're created. These groups could be named anything from "type" to "profile", and there's a ...

Is there a way to specifically execute a Mongoose validate function solely for the create user page and not the edit user page?

Exploring Different Tools In the process of developing a website using Node.js, Express, and MongoDB. Leveraging mongoose for interacting with the MongoDB server has been quite beneficial. However, I encountered an issue where a function within my Mongo ...

Incorporating a swisstopo map from an external source into an Angular

I am looking to integrate a swisstopo map into my angular 8 app. As I am new to angular, I am unsure how to include this example in my component: I have tried adding the script link to my index.html file and it loads successfully. However, I am confused a ...

At times, AngularJs template tags fail to evaluate

Can anyone explain why the templating tag [[ opt.option ]] is not always evaluating to a value in this code snippet? <span ng-repeat="opt in options"> <button ng-click="button = [[ opt.option ]]" ng-class="{ active : button == [[ opt.option ]] ...

Making sure that res.download() is called only after a specific function has finished executing

In my current project, I am utilizing Express.js and HTML to handle the process of retrieving, processing, and then downloading data to a file upon clicking the submit button in an HTML form. The issue I am facing is that the res.download() function withi ...

What are some ways to create a dynamic child server component?

Take a look at the following code snippet // layout.tsx export default function Layout({children}: any) { return <div> {children} </div> } // page.tsx export const dynamic = "force-dynamic"; const DynamicChild = dynamic( ...

React JS displayed the string of /static/media/~ instead of rendering its markdown content

When I looked at the material UI blog template, I created my own blog app. I imported a markdown file using import post1 from './blog-posts/blog-post.1.md'; Next, I passed these properties to this component like so: <Markdown className=" ...

Glitch in transmitting server data to client in MERN stack development

I am currently developing a MERN web application and I've encountered a bug in which the data retrieved from the server is not matching what is expected when making a GET request on the client side. Below is the controller function on the server for ...