[英]How to display a date in <input type = "date"> field when using Angular
I want to get some inputs values from the user by using a form and save it in the database.我想通过使用表单从用户那里获取一些输入值并将其保存在数据库中。 Then I want to display the saved data using this same form.
然后我想用同样的形式显示保存的数据。 But even though I get the values from these input fields I can not display the retrieved values here;
但是即使我从这些输入字段中获取值,我也无法在此处显示检索到的值;
<input type="date" name="ArrivalDate" #ArrivalDate="ngModel" [(ngModel)]="service.formData.ArrivalDate" class="form-control">
And和
<input type="date" name="DepartureDate" #DepartureDate="ngModel" [(ngModel)]="service.formData.DepartureDate" class="form-control">
Screenshot: Display retrieved data from the database but date fields are not filled with the necessary data屏幕截图:显示从数据库中检索到的数据,但日期字段未填充必要的数据
/ ----- code segment ----- / / ----- 代码段 ----- /
<div class="form-row">
<div class="form-group col-md-4">
<div class="form-group">
<label>Arrival Date</label>
<!--I want to get the date input from the user-->
<!--I want to display the date input which given by the user-->
<input type="date" name="ArrivalDate" #ArrivalDate="ngModel" [(ngModel)]="service.formData.ArrivalDate" class="form-control">
<div class="validation-error" *ngIf="ArrivalDate.invalid && ArrivalDate.touched">This field is required.</div>
</div>
</div>
<div class="form-group col-md-4">
<div class="form-group">
<label>Departure Date</label>
<!--I want to get the date input from the user-->
<!--I want to display the date input which given by the user-->
<input type="date" name="DepartureDate" #DepartureDate="ngModel" [(ngModel)]="service.formData.DepartureDate" class="form-control">
<div class="validation-error" *ngIf="DepartureDate.invalid && DepartureDate.touched">This field is required.</div>
</div>
</div>
<div class="form-group col-md-4">
<div class="form-group">
<label>Star Category</label>
<input name="StarCategory" #StarCategory="ngModel" [(ngModel)]="service.formData.StarCategory" class="form-control">
<div class="validation-error" *ngIf="StarCategory.invalid && StarCategory.touched">This field is required.</div>
</div>
</div>
</div>
You can try like this.你可以这样试试。 This is a minimal form with 2 date field.
这是具有 2 个日期字段的最小形式。 You can add the rest of the fields.
您可以添加字段的 rest。
in component template在组件模板中
<form [formGroup]="dateForm" (ngSubmit)="onSubmit()">
<label>
Arrival Date:
<input type="date" formControlName="ArrivalDate">
</label>
<label>
Departure Date:
<input type="date" formControlName="DepartureDate">
</label>
<input type="submit" />
</form>
<label> Arrival Date: {{dateForm.get("ArrivalDate").value}}</label>
<label> Departure Date: {{dateForm.get("DepartureDate").value}}</label>
in component在组件中
import { FormGroup, FormControl } from "@angular/forms";
arrivalDate : Date = new Date();
departureDate : Date = new Date();
dateForm = new FormGroup({
ArrivalDate: new FormControl(this.arrivalDate.toISOString().split("T")[0]),
DepartureDate: new FormControl(this.departureDate.toISOString().split("T")[0])
});
onSubmit(){
console.log(this.dateForm.value);
}
working sample in stackblitz: https://stackblitz.com/edit/angular-xkgxxt stackblitz 中的工作样本: https://stackblitz.com/edit/angular-xkgxxt
The issue is that the default datepicker of the browser can display date strings (not date objects) with the date section only (without the time).问题是浏览器的默认日期选择器只能显示带有日期部分的日期字符串(不是日期对象)(没有时间)。
You need to convert your dates to strings first, like in this example: https://stackblitz.com/edit/angular-zpntay?file=src/app/hello.component.ts您需要先将日期转换为字符串,如下例所示: https://stackblitz.com/edit/angular-zpntay?file=src/app/hello.component.ts
@Component({
selector: 'hello',
template: `
<form [formGroup]="myForm">
<input type="date" formControlName="dateOfBirth">
</form>`,
})
export class HelloComponent implements OnInit {
myForm: FormGroup;
constructor(private fb: FormBuilder) {}
ngOnInit() {
this.myForm = this.fb.group({
dateOfBirth: new Date('1988-05-15').toISOString().split('T')[0]
});
}
}
I changed;我变了;
<input type="date">
to <input type="datetime">
<input type="date">
到<input type="datetime">
Then I could successfully display the date and time.然后我可以成功显示日期和时间。
Issue: When I do the insertion, arrival date & departure date stored in the database like this.问题:当我像这样存储在数据库中的插入、到达日期和离开日期时。 '2020-05-01T00:00:00' When I retrieved data I used to display it using
<input = "date">
field, so I changed it to <input type= "datetime">
'2020-05-01T00:00:00' 当我检索数据时,我曾经使用
<input = "date">
字段显示它,所以我将其更改为<input type= "datetime">
<input class="form-control" type="date" (change)="SendDatetoFunction($event)" name="date" id="date">
#function in angular ts file #angular ts文件中的函数
SendDatetoFunction(event: any) {
console.log(event.target.value);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.