raj_tx
raj_tx

Reputation: 239

How to get the date as in required format: ionic 4

I want to declare the date in the below code in the format of (yyyy mm dd hh mm ss).

postData(data) {
    let headers = new HttpHeaders();
    headers.append('Content-Type','application/json');
    var pingData = {
        date: new Date(year, month, day, hours, minutes, seconds, milliseconds)
    }
}

When I declare it as shown in the above a code, I'm getting the error as

Cannot find name 'year'. Cannot find name 'month'.....

I want the date variable to be string type. So, how can I get the date as I mentioned in the above format?

Upvotes: 1

Views: 3694

Answers (2)

taiwo sunday
taiwo sunday

Reputation: 33

use this format it will work var d = new Date(2018, 11, 24, 10, 33, 30, 0);

Upvotes: 1

Praveen Kumar Tripathi
Praveen Kumar Tripathi

Reputation: 290

Try This Maybe Helpfull for you

in AppModulePage

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

providers: [DatePipe]

in demo.component.ts

import { DatePipe } from '@angular/common';
.
.
constructor(private datePipe: DatePipe) {}

ngOnInit() {
   var date = new Date();
   console.log(this.datePipe.transform(date,"yyyy mm dd hh mm ss")); 
}

Upvotes: 2

Related Questions