简体   繁体   English

如何在发布请求之前首先将 FormGroup 日期转换为 systimestamp

[英]How to first convert FormGroup date to systimestamp before post request

I have date of systimestamp(2020-10-20 23:16:24.727272) in my object which come from the DB which I intend to edit.我的对象中有 systimestamp(2020-10-20 23:16:24.727272) 的日期,它来自我打算编辑的数据库。 .substring(0, 10) was used so on the edit form I got 2020-10-20, now after I edited the data and submited the form, I want the date to be converted back to the systimestamp(2020-10-20 23:16:24.727272) format on POST request. .substring(0, 10) 在我得到的编辑表单上使用了 2020-10-20,现在在我编辑数据并提交表单后,我希望将日期转换回系统时间戳(2020-10-20 23:16:24.727272) POST 请求格式。

I already read on date conversion but the question here is if it is possible to access the 2020-10-20 coming from FormGroup and convert it before calling post request.我已经阅读了日期转换,但这里的问题是是否可以访问来自 FormGroup 的 2020-10-20 并在调用 post 请求之前转换它。

comp.ts比较

//data to be edited
{
"game": "away",
"date": "2020-10-20 23:16:24.727272"
}

//show data on form
gameDetail(data: any) {
  this.formData.controls.game.setValue( data.game );
  this.formData.controls.date.setValue( data.date.substring(0, 10) );
}

//after edit and submit button
postEdit() {
        // ....is it possible to convert or .getTime() the data here before update. 
        //The "this.formData.value" holds the object new values
  this.service.update( this.formData.value )
    .subscribe( data => {
         this.data = data;
     }
}

html html

<form [formGroup]="formData" (ngSubmit)="postEdit()">
   //.......

I want to know how to access the form date for manipulation..我想知道如何访问表单日期进行操作..

I'm with ZetaPR for using a sensfull date-time-library.我和 ZetaPR 一起使用一个 sensfull 日期时间库。 I personally like moment.js.我个人喜欢moment.js。

This is a possible solution:这是一个可能的解决方案:

 import {Component, OnInit} from "@angular/core"; import {FormBuilder, FormControl, FormGroup} from "@angular/forms"; import {Moment} from "moment"; import * as moment from "moment"; @Component({ selector: "app-root", templateUrl: "./app.component.html", styleUrls: ["./app.component.scss"] }) export class AppComponent implements OnInit { data: IData; formGroup: FormGroup; constructor( private service: any, private fb: FormBuilder, ) { } ngOnInit() { this.data = this.gameDetail({ game: "away", data: "2020-10-20 23:16:24.727272" }); this.formGroup = this.fb.group({ gameCtrl: new FormControl(this.data.game), dateCtrl: new FormControl(this.data.date.format("yyyy-MM-dd")) }); // here your data object is updated by the formGroup this.formGroup.controls.gameCtrl.valueChanges .subscribe(value => this.data.game = value); this.formGroup.controls.dateCtrl.valueChanges .subscribe(val => this.data.date = moment(val)); } postEdit() { // Your data object is already updated const dataToPost = { game: this.data.game, date: this.data.date.format("yyyy-MM-dd hh:mm:ss:ms") // hope this is the right pattern. check momentjs.com }; this.service.update( dataToPost ) .subscribe( data => { this.data = data; }); } gameDetail(data: any) { return { game: data.game, date: moment(data.date) }; } } export interface IData { "game": string; "date": Moment; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM