如何改变输入类型=“日期"的输出日期格式?在角4? [英] How to change the output date format of input type="date" in angular 4?
问题描述
实际上,我正在使用angular 4应用程序.我遇到的情况是,我必须将 dd/mm/yyyy 的日期发送到服务器.
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.
我正在使用属性作为输入类型="date".但是此属性返回的值类似于 yyyy/mm/dd .那么如何更改日期的输出格式.
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">
选择日期后,当我用console.log()检查时.
After selecting the date, when I am check with console.log().
Students.components.ts
checkDate() { console.log(Students.dob); }
最后输出为 yyyy/mm/dd..
有什么办法可以解决这个问题?请告诉我.
Is there any way to overcome this issue? kindly let me know.
推荐答案
您可以使用checkDate()
内的 DatePipe
将日期更改为 dd/mm/yyyy
格式功能.像下面一样.并将该日期发送到服务器端.
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.
首先将 DatePipe
导入您的组件
first import the DatePipe
into your component
import { DatePipe } from '@angular/common';
然后按如下所示使用它
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.
希望这对您有帮助!
这篇关于如何改变输入类型=“日期"的输出日期格式?在角4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!