spring - 以 Angular 更改日期输入字段格式

标签 spring angular web-services date

我有一个编辑表单的输入字段,当我单击“编辑”时,它应该会给我一个日期。从我的 Spring 后端开始,我得到了一个长格式的日期,例如。 164888330000。如果我将输入类型更改为文本,我将获得日期的长值,但我想获得格式为 yyyy-mm-dd 的日期,因为这是我存储日期的方式。不幸的是,据我所知, Angular 管道不能用于输入

<label class="form-control-label" *ngIf="truckForm.get('date').invalid && processValidation"
                       [ngClass]="'error'"
                       for="purchasedDate">date is required.</label>
                <input formControlName="date"
                       id="purchasedDate"
                       class="form-control"
                       type="date"
                       name="purchasedDate"
                       [(ngModel)]="truck.purchasedDate"/><strong>Purchased Date</strong>
              </div

我在我的组件类中使用了 formGroup。在 UI 中显示日期之前,有没有办法将日期转换为我想要的格式?在 angular.js 中,可以使用 new Date(long) 并将其转换,但我不确定如何在 angular 4 中实现这一点

  @Input() truck: Truck;
 truckForm = new FormGroup({
    truckCode: new FormControl('', Validators.required),
    date: new FormControl('', Validators.required),
    descriptions: new FormControl('', Validators.required)
  });
 processForm() {

    this.processValidation = true;
    if (this.truckForm.invalid) {
      return; //Validation failed, exit from method.
    }

    // if we are here then all good
    this.preProcessConfigurations()

    let truckCode = this.truckForm.get('truckCode').value.trim();
    let date = this.truckForm.get('date').value.trim();
    let description = this.truckForm.get('descriptions').value.trim();

    //this.dateString = new Date(this.truck.purchasedDate);

    let truck = new Truck(this.truck.truckId, truckCode, date , description);

    this.truckService.updateTrucks(truck).subscribe(successCode => {
      this.statusCode = successCode;
      this.router.navigate(['/trucks']);
    }, errorCode => this.statusCode = errorCode);


  }

这是一个更新功能,所以我想以 yyyy-mm-dd 格式接收日期,关于如何实现该功能的任何帮助?

最佳答案

我不确定您在寻找什么,但是,如果您使用 json,则可以在属性 Date of spring 中使用注释 @JsonFormat (pattern = "yyyy-MM-dd")

@JsonFormat(pattern = "yyyy-MM-dd")
private Date date;

关于spring - 以 Angular 更改日期输入字段格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49910533/

相关文章:

web-services - 如何使用 SOAP 在 Node.js 中调用 ucf Web 服务

java - 在 Spring MVC 应用程序中使用 Java 枚举?

java - 带有单页 angular2 重定向的 Spring Boot

java - 如何使用 AuthenticationSuccessHandler 将用户重定向到页面?

javascript - 向 Kendo UI 上下文菜单项添加其他数据

java - 如何在 Glass Fish Server/Apache Tomcat 上部署 Rest Web 服务?

java - 如何防止在事务提交时保存到 Hibernate 中的数据库?

angular - CORS 环境中未发送 Session-Cookie

即使我声明导出,Angular <component> 也不是模块的已知元素

java - 如何在 Spring Restful Webservice 中接受 JSON 输入?