How to change the output date format of input type="date" in angular 4?
Asked Answered
M

5

8

Actually I am working with angular 4 application. I am having a scenario like, I have to send the Date for as dd/mm/yyyy to the server.

I am using property as input type ="date". But this property returns the value like yyyy/mm/dd. so how do I change the output format of the Date.

Students.html

<input type="date" [(ngModel)]="Students.dob" name="Students.dob">

After selecting the date, when I am check with console.log().

Students.components.ts

checkDate() { console.log(Students.dob); }

Finally the output was yyyy/mm/dd..

Is there any way to overcome this issue? kindly let me know.

Megaron answered 21/5, 2018 at 9:6 Comment(0)
R
7

You could change the date into dd/mm/yyyy format using DatePipe inside the checkDate() function. like below. And send that date into the server side.

first import the DatePipe into your component

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

then use it like below

  checkDate() {
    const dateSendingToServer = new DatePipe('en-US').transform(this.Students.dob, 'dd/MM/yyyy')
    console.log(dateSendingToServer);
  }

working example you could be found here on STACKBLITZ DEMO.

Hope this will help to you!

Radish answered 21/5, 2018 at 10:9 Comment(0)
F
2

You could try this -

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

checkDate() {
    let formatedDate = new DatePipe().transform(this.Students.dob, 'dd/mm/yyyy')
    console.log(formatedDate); 
  }
Fulgurate answered 21/5, 2018 at 9:16 Comment(1)
is there any solution to work this method <input type="date" [(ngModel)]="Students.dob|date:'dd/MM/yyyy'" name="Students.dob">Megaron
S
0

You can use DatePipe method.

<input type="date" [(ngModel)]="Students.dob|date:'dd/MM/yyyy'" name="Students.dob">

You can read more from here. https://angular.io/api/common/DatePipe

Studio answered 21/5, 2018 at 9:16 Comment(4)
When I use the Pipe expression in the ngModel shows error like "Cannot have a pipe in an action expression at column 16 in [students.doj | date:'dd/MM/yyyy'=$event] in"Megaron
@VigneshMohanraj If you one-way bind the model like [ngModel]="Students.dob|date you can use this.Ikeda
But the question specifies 2-way data bindingIgnacia
you shuold use [ngModel] = "Students.dob|date" and (onModelChange)="Students.dob=$event" Hubing
S
0

You can use moment.js. Also, you need to install and import it.

npm install moment --save
import * as moment from 'moment'; //in your component

then use

checkdate(){
   const date = moment(Students.dob).format(DD-MM-YYYY);
   console.log(date);
}
Sylvanite answered 28/1, 2021 at 8:34 Comment(0)
H
0

I had the same problem in the html templates and solved it like this:

  1. Go to your app.module.ts search for the providers and insert DatePipe
  2. go into the html template where you want to change the date format and change it like that {{Students.dob | date:'dd/MM/yyyy'}}
  3. Save the two edited files and check how you date is displayed :)
Hafner answered 10/8, 2023 at 17:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.