What is the best way to retrieve the date and time in mm-dd-yyyy hh-mm-ss format using typescript?

Right now, I am receiving date and time in the format 'mm/dd/yyyy, hh:mm:ss'. Is there a way to retrieve it in this format 'mm-dd-yyyy hh-mm-ss' without relying on any external libraries, and by potentially passing certain arguments to the function itself?

Here is the code snippet that I am currently utilizing (within Angular 5):

console.log(new Date().toLocaleString(undefined, { hour12: false }));

Answer №1

Utilize the DatePipe functionality provided by Angular framework

{{ strDate | date :'MM-dd-yyyy hh-mm-ss' }

Alternatively, in your code you can implement it like this

import { DatePipe } from '@angular/common';

@Component({
 selector: 'test-component',
  templateUrl: './test-component.component.html'
})
class TestComponent {

  constructor(private datePipe: DatePipe) {}

  formatDate(date= new Date()) {
    return this.datePipe.transform(date,'MM-dd-yyyy hh-mm-ss' );
  }
}

Check out more information here : DatePipe

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

Is it possible to trigger a keydown event using a forEach loop?

I've been working on implementing an accessibility feature for a bootstrap accordion. I included the tabindex=0 in each <a> tag in an attempt to make it so that when the user presses the tab key, it opens the corresponding dropdown/accordion. Ho ...

Determining the completion of multiple asynchronous calls in AngularJS: A guide

I have a SQLService running on my PhoneGap/AngularJS app that processes a long array of Guidelines with DB transactions. How can I indicate when the final transaction is completed? I envision a flow like this: In the controller, call `SQLService.ParseJS ...

Guide to preserving canvas state in React?

I am currently working on a drawing application that allows users to draw lines on a canvas. The functionality is such that the line starts drawing on the first click, continues as the mouse moves, and stops on the second click. However, each time a user ...

Dynamic jQuery slideshow featuring a variety of animations applied to individual elements

I'm interested in creating a custom jQuery slideshow that involves animating HTML elements within each slide differently. Specifically, I would like to have 3 divs sliding out to the left with delays on 2 of them, and then being replaced by equivalen ...

Error: 'error' is undefined

Error Alert: The code is encountering a ReferenceError, indicating that 'error' is not defined in the following snippet: app.post('/register', function(req, res) { var hash = bcrypt.hashSync(req.body.password, bcrypt.genSaltSync(10)) ...

Patience is key for a fully completed JSON in JavaScript

I recently came across a similar discussion on Stack Overflow, but it involved using JQuery which I'm not using. My issue is that I need to ensure my JSON data is fully loaded before calling my function. I understand the concept of using a callback, ...

Mastering the Art of Nodemailer

Can anyone advise on how to utilize nodemailer with dynamic email and password retrieved from a database? export const mailTransporter = nodemailer.createTransport({ service: 'gmail', auth: { user: email_user, pass: email_pass, }, } ...

What is the process for using a formula to establish disabled dates for a datepicker?

Below is the code snippet in question: if (v.name == 'WISH_DATE') { const statusDate = moment(this.validAddress.duedate, "DD/MM/YYYY"); const minDate = moment(statusDate).add(5, 'days'); const max ...

Utilize recursive and for loop methods for parsing JSON efficiently

I have a JSON file that requires parsing. I'm attempting to implement a recursive method for this task. The current JSON data is structured as shown below: Item 01 SubItem 01 InnerSubItem 01 Item 02 SubItem 01 InnerSubItem 01 Unfortunately, t ...

Passing data into a different controller

In the process of building a system that involves selecting elements from a list and viewing their details, I have encountered an issue while using MEAN stack. My goal is to pass the id of the selected element to the controller of the second page. However, ...

Ensure that the JavaScript is loaded prior to refreshing the webpage

Below is the HTML code snippet: <script src="https://maps.googleapis.com/maps/api/js?key=mykey&callback=initMap&v=quarterly" defer></script> <script src="{% static '/javascript/maplanguage.js' %}">< ...

Troubleshooting the "Eslint not defined" error when importing modules

My App.js file includes: import './bootstrap'; if (document.getElementById('vue')) { new Vue({ }); } This code imports a bootstrap javascript file that contains the Vue npm package(node module). In my bootstrap file, I impor ...

The hyperlinks in the navigation bar are leading me to incorrect destinations

I am currently developing an authentication app and facing an issue with the navbar links for register and login. They are not redirecting me to the correct destination. These links work perfectly on the landing index page, but not on any other page. site ...

Mixing resource and non-resource techniques in Angular factory

Feeling a bit confused by what should be a simple task. I am currently working on enhancing an Angular service by adding some new methods. Initially, the service was only making a single $resource call to my API. The additional methods I am incorporating a ...

Generating table columns using *ngFor method

Trying to create a table in Angular and wondering how to generate table columns using something like *ngFor. I currently have two lists: Data: [ 0: data: [ 0: 0 1: 4 2: 4 3: 3 4: 5 5: 1 ] id: 49 label: "Label_1" 1: data:[ 0: 5 1: 0 2: 1 3: 5 4: 0 ...

Are there any reliable charting APIs available that can efficiently handle the lazy-loading of large data sets using Ajax while scrolling or zooming?

Hey there! I need help finding a JavaScript charting API that allows me to query the server using specific bounds on the horizontal and vertical axes in order to retrieve new data points. Just to give you an idea, the dataset I'm working with is incre ...

Using React to apply various filters to an array

Just getting started with react and working with an array of objects named arrayToFilter that I need to filter in multiple ways. Whenever a user changes the filter options, all filters should be applied to the array and the results should be stored in a fi ...

How can successful AJAX requests access PHP data?

Do you know how I can run a basic PHP code using AJAX and receive the data back from the PHP page in the AJAX success function? I am facing an issue where I am not getting anything in the AJAX success callback from the PHP page, and it's really bothe ...

Employing the $(this) in jQuery to modify a separate element

Hey, I have a question about changing attributes in jQuery. I already know how to change an attribute for the same element you hover over... $(".click2play").mouseover(function() { $(this).css({'visibility' : 'hidd ...

I'm encountering issues with undefined parameters in my component while using generateStaticParams in Next.js 13. What is the correct way to pass them

Hey there, I'm currently utilizing the App router from nextjs 13 along with typescript. My aim is to create dynamic pages and generate their paths using generateStaticParams(). While the generateStaticParams() function appears to be functioning corre ...