【问题标题】:When creating a date from to is not taking the date given当创建从 to 的日期不采用给定的日期时
【发布时间】:2018-03-28 20:52:01
【问题描述】:

我有一个角度为 4 的输入类型日期,并且我有 2 个日期选择器字段,例如,当我从 23.03.2018 选择到 29.03.2018 时,当我按下保存时,日期取的是今天的日期,这不会改变,这怎么可能发生。我尝试使用 ngModelChange 来更改该值,但在数据库中它发送的就像今天的日期一样...您可以在下面找到代码。

    <app-input-field orientation="top" label="Beginning Date">
        <input [min]="today" type="date" step="1" [(ngModel)]="newCreate"  style="width:250px">
      </app-input-field>
        <br>
        <app-input-field orientation="top" label="Ending Date">
          <input [min]="today" type="date" step="1" 
 [(ngModel)]="newEnd" style="width: 250px;">
        </app-input-field>

这是 TS 文件

  newCreate: string = new Date().toISOString().substring(0, 19);
  newEnd: string = new Date().toISOString().substring(0, 19);

这是我点击保存的时候。

save() {

// How to convert here because when is saved it is saved like date 
//with minutes and everything else.
    this.newProject.name = this.newProjectName;
    this.newProject.state = this.newState;
    this.newProject.type = this.newType;
    this.newProject.created = new Date(this.newCreate);
    this.newProject.ending = new Date(this.newEnd);
    this.newProject.category = this.category;
    this.newProject.subProjectIds = this.subProjectIds;
    this.store.dispatch(new UpsertProjectAction(this.newProject));
    this.dialogRef.close(this.newProject);

【问题讨论】:

  • 你可能希望用你正在使用的库来标记它。不同的日期选择器组件具有不同的行为。例如,有些要求值是Date 对象而不是字符串。另外,您需要检查Converting string to date in js

标签: angular typescript


【解决方案1】:

在您的.ts 文件中,您将newCreatenewEnd 设置为当前日期。

如果没有提供参数,构造函数会创建一个 JavaScript 根据系统设置的当前日期和时间的日期对象 时区偏移量。

JavaScript - Date - MDN Docs

通过使用[(ngModel)],您将使用双向数据绑定,并且在初始化时,您的日期将自动设置为当前日期。

查看这个有效的 StackBlitz:Working Demo

【讨论】:

  • 我该如何解决这个问题你知道吗?
  • 日期选择器是返回字符串还是日期?
  • 我尝试在 StackBlitz 中重现您的问题,但它按预期工作。我无法重现您的问题。您能否提供可重现该问题的 StackBlitz 或代码示例?
  • 这一切都与接口和实用程序有关,在我创建的接口处:日期;并在创建的 utils 中:new Date()
  • 看看我的 StackBlitz,你有没有这样定义你的属性?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-03
  • 1970-01-01
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多