How to convert a string in dd-mm-yyyy format into a date in Typescript

I am receiving data from a datetimepicker in the format "14-02-2018" as a string value. I need to parse this value into a date object. I attempted to do it like this:

new Date(Date.parse('02-03-2018))

However, this only works if the format is '2018-03-02'. I also tried using Moment.js but encountered an issue with parsing:

var aaa = moment(itemvalue, "DD-MM-YYYY");

Answer №1

Utilizing Moment JS Library

let currentDate = new Date(moment('02-03-2018', "DD-MM-YYYY"));

console.log(currentDate.toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>

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 are the steps to retrieve the original source code of an HTML file, specifically in Angular 2 RC4

Is there a way to retrieve the source code that I manually typed in my IDE using JavaScript? Note: I am working with angular2 rc4. I attempted to access it using Reflect.getMetadata, but encountered errors indicating that it is not functioning properly. ...

I'm struggling to find the right Typescript syntax for defining a thunk function that returns a value while using React Redux Toolkit

Currently, I am utilizing TypeScript within a React Redux Toolkit project. While attempting to create an Async Thunk action function that is expected to return a boolean value, I found myself struggling with determining the correct TypeScript syntax: expor ...

The functionality of CDK Drag Drop is not accurately adjusting the placement of images

I have implemented an image gallery and am working on rearranging the position of the images using the Drag & Drop cdk library. However, I am facing an issue where the swapping of images does not always occur correctly; sometimes when attempting to exchan ...

Handling date formatting problems within C#

Can anyone help me convert a date in C# from Javascript? The date is currently in the format: "Tue Jan 15 00:00:00 UTC+0530 2008". How can I change it to "dd/MMM/yyyy"? ...

Unexpected behavior with AWS DynamoDB ScanInput ExpressionAttributeValue

I crafted a scan query to only retrieve enabled data in the following way: const FilterExpression = 'enabled = :enabled'; const ExpressionAttributeValues = { ':enabled': { 'BOOL': true } }; const scanParameters: Sc ...

The interface does not allow properties to be assigned as string indexes

Below are the interfaces I am currently working with: export interface Meta { counter: number; limit: number; offset: number; total: number; } export interface Api<T> { [key: string]: T[]; meta: Meta; // encountered an error here } I h ...

Ways to maintain data returned by forkJoin

I encountered a situation where I needed to retrieve results from 2 HTTP calls and combine them into an array that would be passed to a class instantiation. Below is the code snippet: export class VideoListComponent implements OnInit { @ViewChild(MatPag ...

Using a TypeScript decorator to enhance a class with a new property

I'm in the process of developing a custom decorator that adds a specific property to the target class. Here is my current implementation for the decorator: export type Constructable<T> = new (...args: any[]) => T; export function Validation ...

What is the proper way to invoke a child method after converting an object from a parent class to a child class?

When we have a subclass B that overrides a method from its superclass A in TypeScript, why does calling the method on an instance of A result in the parent class's implementation being called? In TypeScript, consider a class called Drug with properti ...

String converted to an unknown number surpassing the null validation

I am facing a simple issue that has me stumped. I am passing a parameter to an express http get function, which is then used in a query. To prevent SQL injection, I ensure that the parameter is a number. However, due to the data structure of my client, I ...

Avoid unnecessary re-rendering of React Native components with each update of the state

I need my component to display either A or B based on the user's proximity to a specific location. I developed a custom hook to determine if the user is nearby. However, I'm facing an issue where the hook constantly returns a new value of true, ...

Styling Ion-Slides in Ionic 3 with Inline CSS

On the main page of my Ionic3 App, I have an ion-slides component. <ion-slides pager> <ion-slide *ngFor="let blog of blogs | async" > <h2>{{ blog.title }}</h2> <p> <a target="_blank" class="slider-rea ...

Is there a way to compare timestamps in PostgreSQL using moment.js and focus only on the date aspect?

I've encountered a small issue that has me stumped - I'm trying to figure out the best solution for it. The problem lies in a table I have, with attributes named "Start" and "End". My objective is to store every row in an array where the "Start" ...

Retrieve a HashMap through an HTTP GET request using Angular 10

I am currently using the following versions: Angular CLI: 10.0.1 Node: 12.18.2 OS: win32 x64 Angular: 10.0.2 In my setup, I have a Java Spring Boot service that is functioning correctly and returns data as a HashMap. Map<String, List<String>&g ...

Mastering the proper implementation of OneToMany and ManyToOne relationships in MongoDB involves understanding and utilizing the

I am currently working on setting up a oneToMany, ManyToOne relation method and here is my progress so far (pseudocode provided below). I am using Typegoose which is essentially Mongoose with types. If you are unfamiliar with it, that's okay because t ...

Tips for refreshing the 'state' of an Angular Router component

I'm facing an issue with passing data between pages using Angular routing. Everything works as expected when navigating to the "next" page for the first time. However, upon returning to the home page and selecting a different item, the Router's s ...

Assigning properties of an object with a mix of data types dynamically in TypeScript

Imagine you have an object with properties that are of different types, and you need to assign a dynamic value to a property identified dynamically. While this is easily done in JavaScript, how can you accomplish the same in TypeScript without encountering ...

Dependency injection in Angular is a powerful design pattern that

I recently completed an Angular 2 website, but I've run into some issues that I cannot seem to debug. I've spent the past week trying to find a solution but have had no luck. Below, I've included some snippets of my code. Feel free to ask fo ...

Algorithm to assess date equivalence and provide a boolean response to the calendar

Currently, I am working on implementing a logic for a calendarpicker component. This component includes a disabledDateCallback event parameter that provides a date as input. My task is to develop a logic that will allow me to disable all dates retrieved ...

Is it possible to access ng-content components using @ViewChild?

I have utilized a card template that employs ng-content slots in the following manner: <ng-content select="card-header"></ng-content> <ng-content select="card-body"></ng-content> <ng-content></ng-c ...