Transform the object into a date once it initiates at 1

After integrating the angular bootstrap datepicker component into my application, I encountered an issue with converting the selected date object {year: 1993, month: 8, day: 9} into a Date object. The conversion is successful, but the resulting date is shifted: 1993-09-08T22:00:00.000Z. Any suggestions on how to maintain the original date during conversion?

Upon investigation, I discovered that this discrepancy is due to the difference in the starting date numbers between angular bootstrap (starting from 1) and the Date object (starting from 0).

For the HTML setup:

<div class="col-md-12 mb-3 form-group">
    <label for="dateOfBirth">Data urodzenia</label>

    <input class="form-control"
        placeholder="yyyy-mm-dd" id="dateOfBirth" 
        formControlName="dateOfBirth"
        name="dp"
        ngbDatepicker
        #d="ngbDatepicker"
        (click)="d.toggle()" 
        [minDate]="fromDate"
        [maxDate]="toDate">

    <span class="input-group-addon">
        <i class="glyphicon glyphicon-calendar"></i>
    </span>

    <ngb-alert [type]="'danger'"
        [dismissible]="false" 
        class="form-alert"
        *ngIf="envelopeCreationFrom.controls.dateOfBirth.touched && 
        envelopeCreationFrom.controls.dateOfBirth.errors?.required">
        Error
    </ngb-alert>
</div>

And for the Typescript code:

 console.log(dateOfBirth);
 console.log(new Date(dateOfBirth['year'], dateOfBirth['month'], dateOfBirth['day']));
 console.log(new Date(dateOfBirth['year'], dateOfBirth['month'], dateOfBirth['day']).toISOString());
 console.log(new Date(dateOfBirth['year'], dateOfBirth['month'], dateOfBirth['day']).toDateString());

Answer №1

If you are working with dates that require any type of calculation or formatting, I suggest utilizing moment.js.

First, make sure to install moment.

npm install moment --save

Then, simply import it into the component where it is needed.

import * as moment from 'moment';

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

Column Overflow with Bootstrap 4 File Input Label

How can I ensure that the label for a file input in my Bootstrap 4 form fits within the column without overflowing? Are there any Bootstrap classes or custom CSS tricks to achieve this? Edit: To clarify, I am referring specifically to the "Choose file..." ...

Integrating a neighborhood library with an Angular 5 Project

I want to have a local library that can reflect changes in the project using it. I have cloned the library from this link on my local machine: https://github.com/manfredsteyer/angular-oauth2-oidc Currently, I am navigating to the library directory and run ...

JHipster form validation is failing to function properly

I have a specific class structure that is defined as follows in JDL entity Address { address String required, city String required, postalCode String required, country String required, } entity MainEntity { <some fields> } relationship O ...

What are the benefits of utilizing local JSON data in conjunction with useEffect in React?

Encountering the following error message: TS2345: Argument of type '({ Alcohol: number; "Malic Acid": number; Ash: number; "Alcalinity of ash": number; Magnesium: number; "Total phenols": number; Flavanoids: number; &qu ...

Issue encountered while attempting to install Angular CLI on Windows: ENOENT Error

After extensive research online, I have been struggling to find a solution to this issue. I've attempted the following command: npm install -g @angular/cli The error message I'm receiving is: https://i.sstatic.net/ACSwc.png Npm version: 7.7.6 ...

Minimize overlap across both projects

One scenario is having two projects that utilize a lot of the same components. How can we minimize redundancy between them? Is there a way to make them shareable? Perhaps leveraging webpack could be the solution? ...

Example of a floating undo bar using a dynamic function in a Vuex store module

Issue Overview Trigger the mutation or action of Vuex store module A to execute an external function. This external function can belong to another Vuex store module (e.g. B). A should store a reference to the external method (e.g. mutation or action from ...

Exploring the potential of Vue with Router to create a dynamic multi-page

Struggling to come up with a suitable title for this Vue project dilemma. Here's what I'm facing: Recently started using Router in my Vue project and feeling quite lost. The setup in App.vue simply includes <RouterView>, which seems stra ...

Retrieve the component's name using ReactElement.type.name

In my current project, I am working with React and Typescript. I need to retrieve the name of a functional component, which I have discovered can be accessed through the 'type' property of the component like this: component.type.name. Initially, ...

waiting for the import statement in a React/NextJS/Typescript project to resolve

While working on my node.js development server, I encountered a problem with the following code: import { useRouter } from 'next/router' import nextBase64 from 'next-base64'; const Load = () => { const router = useRouter() co ...

Exploring Angular: distinguishing between sessions on separate tabs

I am currently working on a web application that utilizes Angular for the front end and a Django Rest API for the back-end. In some cases, I need the Django Rest API to be able to distinguish between polling requests using session IDs. After conducting som ...

Can you explain the significance of syntax in sample code (typescript, react)?

const sampleFunction: (inputString: string) => string = inputString => { return inputString.split(""); } I'm a bit confused about the code below and would appreciate some clarification. I understand that only "string" as a type is accepted, b ...

Jest tests are failing to render React IonDateTime component

While executing Jest on an Ionic React component, I encountered a test failure consistently, regardless of whether the component had a time value or not. test('IonDateTime display', () => { render(<IonDatetime data-testid="foo" ...

pressing the button again will yield a new outcome

I am looking to disable a button (material ui) when it is clicked for the second time by setting disabled={true}. Unfortunately, I have not been able to find any examples related to this specific scenario on StackOverflow. <Button onClick={this.s ...

Determining if an emitted event value has been altered in Angular 4

I am currently working on an Angular 4 project. One of the features I have implemented is a search component, where users can input a string. Upon submission of the value, I send this value from the SearchComponent to the DisplayComponent. The process of ...

Is it possible to minimize the number of accessors needed for reactive forms?

Currently, I am dealing with a reactive form that consists of 20 different inputs. An example of one input is shown below: <input formControlName="name" matInput> For each input, I find myself needing to write an accessor function like the ...

How to dynamically add a route from an HTTP API to the app-routing.module.ts file in Angular 10

Struggling with an issue while working on Angular 10 version. Despite finding numerous solutions for older versions of Angular, I am still unable to resolve the issue. I consider myself a beginner in Angular as I try to create a website using an Angular c ...

Generating typescript definitions for Polymer 2.4 packages

According to information provided in the latest announcement, declarations are now automatically generated from the Polymer source. I recently upgraded to Polymer 2.4 and encountered an error during project build due to a missing typescript definition fil ...

Ways to mimic an Angular subscription during a Jasmine test

I'm currently troubleshooting a unit test for my code (I'm not very experienced with Jasmine) after adding some subscriptions to a service. I'm encountering an issue where the subscriptions are coming up as undefined. I'm not entirely s ...

Angular application experiencing issues with fixed headers not scrolling correctly

I've been working on implementing a fixed header for one of my pages in an Angular application, but I'm having some trouble getting it to work as expected. Currently, when the user expands the accordions on the page and scrolls down, the headers ...