What is the best way to convert the date 19990813 from the format YYYY-MM-DD to 13.08.1999 in the format DD-MM-YYYY, using dots as separators?

Struggling to convert the string date 19990813 provided by the backend into the format 13.08.1999 with dots. Unable to do so successfully.

Even when using Angular's built-in date format function, always ending up with the incorrect date.

Any suggestions on how to accomplish this task would be greatly appreciated.

Thank you in advance for any advice.

Best regards,

Anna Marie

Answer №1

To utilize the moment library, simply run npm i moment and apply it as shown below:

datestring = '19990813';
constructor(){
   let date = moment(this.datestring, 'YYYYMMDD');
   let datedot = date.format('DD.MM.YYYY');
   alert(datedot);
}

Check out a live demo here

Answer №2

You can utilize the String replace() method to achieve this in JavaScript.

let date = `19990813`

let regex = /^(\d{4})(\d{2})(\d{2})$/

let newDate = date.replace(regex, "$3.$2.$1")
console.log(newDate)

Answer №3

If you find yourself needing to transform a string within a component's template, the most efficient approach (for performance reasons) is to utilize a pipe operator.

Follow xyz's suggestion for executing the string transformation process itself.

import {Pipe, PipeTransform} from '@angular/core';

@Pipe({
    name: 'myDateFormat'
})
export class MyDateFormatPipe implements PipeTransform {

    transform(value: string, args?: any): any {
        if (!value) {
            return value;
        } else {
            // implement the transformation logic on the 'value' parameter following xyz's guidance
        }
    }
}

Next, in your template, apply it as follows:

inputDate | myDateFormat

Here, inputDate represents '19990813'.

Remember to import this class into the angular module that will employ it.

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

Understanding Different Symbols in TypeScript

Can you explain the purpose of symbols in TypeScript to me? As someone familiar with Java, it seems a bit unnecessary to use them alongside an interface declaration. What is the reason for setting symbols in TypeScript? For example, consider the followin ...

Getting JSON data from PHP in AngularJS involves making an HTTP request to the PHP file

I've been searching extensively, but haven't found a solution. I need the data from my PHP script to be formatted like this: $scope.places = [{name: 'John'},{name: 'Jane'}]; My issue is figuring out how to make this happen. ...

Error message: "Ajax script is failing to run"

Currently I am delving into the world of Ajax and have encountered a minor issue. I've been attempting to make a POST request to a Django backend using ajax, but strangely enough, the alert isn't showing up on screen. Furthermore, upon inspectio ...

Encountering the error message "Uncaught Error: Objects are not valid as a React child" even though I am not passing objects as children in my React component

My current challenge involves mapping an array of objects, which I retrieved from an axios.get request and then passing them as children to React components. The error message that's causing trouble for me reads as follows: An Error occurred: Objects ...

The Perfect Scrollbar feature is not functioning properly in conjunction with the accordion menu

I am having some trouble with implementing the Perfect Scrollbar alongside an accordion slider menu. The scrollbar is not working as expected, and you can view the fiddle here. On initial page load, the scrollbar only appears for the first sub-menu aft ...

Positioning Images in Tables with Absolute CSS Styling

I need to alternate between two sets of images using JavaScript. To achieve this, I have to use CSS to set the position of each image to absolute so that each new image replaces the old one as it cycles through. My goal is to arrange the image sets in an ...

I'm having trouble accessing the data stored in req.user within the user controller, despite having integrated it into the user isLoggedIn middleware

I am encountering an issue where I cannot access my req.user in the usercontroller, even though I have implemented it in the usermiddleware. Both storing data in req.user and retrieving data using req.user from the middleware to my controller are not work ...

When I refresh the page in Angular2, the router parameters do not get loaded again

When loading my application on routers without parameters, everything is normal. However, when trying to use a router with params, the application fails to load. For example: localhost:3000/usersid/:id The code for the router is as follows: const appRou ...

Having trouble making md-data-table directives function properly

I'm struggling to incorporate the md-data-table library from https://github.com/daniel-nagy/md-data-table into my webpage. Despite loading the library in Chrome, none of the directives seem to be working. Here's a snippet of my code - can anyone ...

Adding JSON to the data attribute within a set of DOM elements

I am in the process of developing a website dedicated to recipes, where I am using a Mustache.js template to load recipe information from a JSON file. The structure of my JSON file is as follows: { "recipes":[ {"name": "A", preparationTime: "40min", "serv ...

Changing the hidden input value to the data-id of a selected <a> element

I've set up a form with a dropdown menu and subdropdowns, generated using a foreach loop through a @Model. When I click on one of the options, I want the value of my hidden input field to match the data-id of the clicked item. This is what I have in ...

Is Ajax capable of producing multiple outputs at once?

I am looking to retrieve data from a product.json file that contains two objects: (1) object for CCTV camera products (2) object for Laptop products. When a specific button is clicked, I want to access the data for LAPTOP products and the same for CCTV. ...

To obtain the desired response, I must make an additional GET request

Upon sending the first GET request to this endpoint, I receive a response of allEvents = []. However, if I wait a few seconds and hit the endpoint again, I get the desired results with a populated allEvents array. It seems that the function is not executin ...

Tips for extracting the authorization token from the response header of an ajax request in AngularJS

Simply put, I have a situation where I make an API call and receive a response that includes the Authorization in the header. I need to extract this authorization token from the header as it is crucial for future API calls. What would be the best way to ...

Dragging the entire ForceDirected Graph in D3.js is not functioning

tag, I am currently working on implementing a D3 force directed graph using D3 v6 and React. The graph includes features such as zoom functionality and draggable nodes. However, as the graph can become quite complex and large due to dynamic data, I aim to ...

Error: Symfony encountered an uncaught type mismatch

Seeking assistance with correcting an Uncaught TypeError related to the tinyMCE editor malfunctioning. https://i.sstatic.net/vM8D5.png I have attempted potential solutions such as: - updating the tinymce installation by replacing the older reference wi ...

Vue.js Components Facing Build Issues

I am currently experiencing a puzzling phenomenon. While working on my application components using Laravel Jetstream and Inertia Stack with VueJS, I encountered an issue where a newly created component in the same folder as others was not building. Neithe ...

Is it possible to activate the jQuery .click() function for a button with specific text?

Here's a dilemma I'm facing: $('.add_to_cart span:contains("Choose a Size")').click(function() { console.log("it has been clicked") }); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></s ...

Sorting by two fields with varying value types in AngularJS

Need help sorting json data by two fields, prioritizing field value and then date. Check out this demo Json input [{ "title" : "Title 2", "pin" : false, "date" : 20130411204207 },{ "title" : "Title 3", "date" : 20140411204207 },{ "title" : "Title 4", ...

Using the default theme in Material UI5

Could someone please explain how to pass in the default theme in Material UI5? In Material UI6, I used to do it like this: const useStyles = makeStyles((theme) => ({ home: { display: "flex", paddingTop: "8rem", width: ...