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.

5 Answers 5

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!

Sign up to request clarification or add additional context in comments.

Comments

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); 
  }

1 Comment

is there any solution to work this method <input type="date" [(ngModel)]="Students.dob|date:'dd/MM/yyyy'" name="Students.dob">
1

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

4 Comments

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"
@VigneshMohanraj If you one-way bind the model like [ngModel]="Students.dob|date you can use this.
But the question specifies 2-way data binding
you shuold use [ngModel] = "Students.dob|date" and (onModelChange)="Students.dob=$event"
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);
}

Comments

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.