角度材料DatePicker:日期变为选定日期前一天

时间:2017-06-25 08:08:07

标签: angular angular-material2

我使用的是最新的Angular 4和Angular-Material2-2.0.0-beta7。这是我使用MdDatePicker的模板:

<md-input-container fxFlex="33" class="birthday-container">
    <input mdInput name="birthday" placeholder="Birthday" required [(ngModel)]="member.birthday" [mdDatepicker]="birthdayPicker">
    <button mdSuffix [mdDatepickerToggle]="birthdayPicker"></button>
</md-input-container>
<md-datepicker #birthdayPicker></md-datepicker>

在app.module中,这是提供者:

{provide: DateAdapter, useClass: NativeDateAdapter}

member.birthdayDate类型。 但是当JSON.stringify(member.birthday)时,它变为所选日期的前一天。例如:

从日期选择器中选择2017-4-1,字符串化结果为2017-03-31T13:00:00.000Z

This post提出了同样的问题,但我不确定如何将moment.js应用于代码。

6 个答案:

答案 0 :(得分:2)

当我们在日期选择器中选择日期时,选择将以GMT(根据格林威治时区)时间进行存储和操作,但会以格式化的表示形式显示给用户,同时考虑用户的当前时区。因此存储和显示的日期是不同的(时区+ 0内的用户除外,如格林威治)。

在对JS Date对象进行字符串化时,我们得到了明确的格林威治时区时间,我们可以在https://time.is/GMT看到它。您可能希望看到的是使用当前时区格式化的时间表示。请参阅我的示例中的差异。

// get current time
let date = new Date();
console.log('NATIVE JS DATE TIME', date.toString());
console.log('STRINGIFIED TIME', JSON.stringify(date));
console.log('FORMATTED TIME', `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`);

您可以使用非常方便的时刻format()功能来显示日期。

答案 1 :(得分:0)

我通过创建一个自定义日期适配器(继承了本地日期适配器)并覆盖了create-date方法来删除时区偏移来解决了此问题

@Injectable({        providedIn: 'root' }) 
export class CustomDateAdapterService extends NativeDateAdapter {    
    public createDate(year: number, month: number, date: number): Date {
        const localDate = super.createDate(year, month, date);
        const offset = localDate.getTimezoneOffset() * 60000;
        return new Date(localDate.getTime() - offset); // utcDate
    } 
}

您可以看看complete gist here

答案 2 :(得分:0)

我们需要了解Date如何工作的一件事。让我看一下如何创建日期对象的示例。

while strtok != null
  strcpy to an array and increment a counter for strings

while read a line
  for each member of the array
    strstr(line, string)

上述日期的输出取决于您传递日期时的时区,就像它被视为GMT一样。它会根据您的时区添加偏移量。假设您的打印速度是+0500

2020年8月22日星期六 05:00:00 GMT + 0500

它将为您的日期增加5小时。

如果您在-0500,那么它将

2020年8月21日星期五 19:00:00 GMT-0500

如果您希望日期始终为您提供通过日期,而无需添加偏移量。

new Date('2020-08-22') // 22 aug 2020(your expect result)

2020年8月22日星期六 00:00:00 GMT + 0500

它不会为日期对象增加一个小时(偏移)。

我只是通过第二种方法解决了这个问题,它对我有用。

答案 3 :(得分:0)

我使用 PHP 和 SQL,我以默认格式存储日期,例如 2021-04-12 00:00:00。 我想存储用户给出的实际值,所以我不用费心转换时区。无关紧要,在写帖子的那天,我只想要一个好的日期表示。

这就是我存储给定日期的当地时间的原因:

let phpDate = `${selectedDate.getFullYear()}-${selectedDate.getMonth()+1}-${selectedDate.getDate()} 00:00:00`;
// Notice that we have to add 1 to the zero-based JS month

解析存储的日期:

this.date = new Date(
        Number(d.substr(0, 4)),
        Number(d.substr(5, 2)) -1, // we need to subtract 1 because JS mont is 0-based
        Number(d.substr(8, 2)));
    })

答案 4 :(得分:0)

我希望这会有所帮助...只需获取 formgroup 的值并存储在 const 中,并在使用 datePipe 后重新分配 formcontrol 的值

import { DatePipe } from '@angular/common';

只需在 component.ts 文件中使用 Datepipe 并添加组件提供程序和构造函数

private datePipe: DatePipe,

providers: [DatePipe]

提交函数内部

const commonForm = {
              ...this.pollAdd.value,
            };
            commonForm.endDate=this.datePipe.transform(this.pollAdd.value.endDate, 'yyyy-MM-dd');
            commonForm.startDate=this.datePipe.transform(this.pollAdd.value.startDate, 'yyyy-MM-dd');

答案 5 :(得分:-1)

使用以下转换将解决您的问题。

  date =  new Date(this.moment(date).utcOffset('+0000').format('YYYY-MM-DD HH:MM'))
相关问题