【问题标题】:How to convert angular material datepicker date to UTC format?如何将角度材料日期选择器日期转换为 UTC 格式?
【发布时间】:2019-11-21 18:39:55
【问题描述】:

我正在接受日期输入并使用角度材料日期选择器,并将其存储在反应形式变量中。 但我希望日期为 UTC 格式,要么输入 UTC,要么输入任何其他格式并将其转换为 UTC。 我该怎么做。

<mat-form-field appearance="outline" fxFlex="row" fxFlex="50%" class="pr-4">
    <mat-label>Invoice Date</mat-label>
    <input type='date' matInput name="" id="" formControlName="invoiceDate" placeholder="Invoice Date" required>
    <mat-icon matSuffix>date_range</mat-icon>
</mat-form-field>

这是我存储日期的表格。它的invoiceDate 变量。

this.newLoanForm = this._formBuilder.group({
      customerId: ['', Validators.required],
      orginizationId: ['', Validators.required],
      salesPersonId: ['', Validators.required],
      invoiceNumber: ['', Validators.required],
      invoiceDate: ['', Validators.required],
      notes: ['', Validators.required]
    })

提前致谢

【问题讨论】:

  • 你试过使用 date-ipe 吗?
  • 什么是 date-ipe ?

标签: angular


【解决方案1】:

使用 moment.js 可以轻松解决问题, 更多信息请参考http://momentjs.com/docs/#/parsing/utc/

   1. npm install moment
    2. import * as _moment from 'moment';
    const moment = _moment;
    const utcDate = moment.utc();
    console.log(utcDate.format());

要解析日期你可以使用moment参考下面的代码

const duration = moment.duration({ 'months': 0 });
if (this.variable33) {
  this.variable10 = moment(this.variable33).subtract(duration).format('YYYY-MM-DD');
  console.log('date changed ', this.variable10);

【讨论】:

  • 我有点困惑。当我做console.log(this.newLoanForm.value.invoiceDate); 时,它给了我一个对象。我现在如何将其转换为 UTC。就像你上面说的。有没有什么简单的功能,当我使用 myDate.toUTC() 时,它给了我 UTC 格式
  • 你能把日期对象的样本发给我吗
  • const isoDate = new Date('yourdatehere').toISOString();
  • 这是在控制台记录我的日期时的输出
猜你喜欢
  • 2019-03-04
  • 1970-01-01
  • 2018-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 2018-08-25
相关资源
最近更新 更多