Skip to content

Commit

Permalink
fix(module:date): all dates supported unit timestamp (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk authored Oct 14, 2018
1 parent 6a68def commit 4c374ad
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- env: "MODE=site"
- env: "DEPLOY_MODE=build"
- env: "DEPLOY_MODE=build_artifacts"
if: (commit_message !~ /(release\()/ && type = push) or (branch = master)
if: (commit_message =~ /(release\()/ && type = push) or (branch = master)

before_install:
- export CHROME_BIN=chromium-browser
Expand Down
4 changes: 2 additions & 2 deletions docs/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ export class I18nTestComponent {

```ts
// #region i18n
import { default as ngLang } from '@angular/common/locales/zh-Hans';
import { default as ngLang } from '@angular/common/locales/zh';
import { NZ_I18N, zh_CN as zorroLang } from 'ng-zorro-antd';
import { DELON_LOCALE, zh_CN as delonLang } from '@delon/theme';
const LANG = {
abbr: 'zh-Hans',
abbr: 'zh',
ng: ngLang,
zorro: zorroLang,
delon: delonLang,
Expand Down
12 changes: 10 additions & 2 deletions packages/form/src/widgets/date/date.widget.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, OnInit } from '@angular/core';
import { ControlWidget } from '../../widget';
import format from 'date-fns/format';
import { ControlWidget } from '../../widget';
import { toBool } from '../../utils';
import { FormProperty } from '../../model/form.property';

Expand Down Expand Up @@ -130,8 +130,9 @@ export class DateWidget extends ControlWidget implements OnInit {
}

reset(value: any) {
value = this.toDate(value);
if (this.flatRange) {
this.displayValue = value == null ? [] : [value, this.endProperty.formData];
this.displayValue = value == null ? [] : [value, this.toDate(this.endProperty.formData)];
} else {
this.displayValue = value;
}
Expand Down Expand Up @@ -171,4 +172,11 @@ export class DateWidget extends ControlWidget implements OnInit {
private setEnd(value: any) {
this.endProperty.setValue(value, true);
}

private toDate(value: any) {
if (typeof value === 'number' || (typeof value === 'string' && !isNaN(+value))) {
value = new Date(+value);
}
return value;
}
}
4 changes: 2 additions & 2 deletions packages/schematics/application/files/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

// #region default language
// 参考:https://ng-alain.com/docs/i18n
import { default as ngLang } from '@angular/common/locales/zh-Hans';
import { default as ngLang } from '@angular/common/locales/zh';
import { NZ_I18N, zh_CN as zorroLang } from 'ng-zorro-antd';
import { DELON_LOCALE, zh_CN as delonLang } from '@delon/theme';
const LANG = {
abbr: 'zh-Hans',
abbr: 'zh',
ng: ngLang,
zorro: zorroLang,
delon: delonLang,
Expand Down
4 changes: 3 additions & 1 deletion packages/theme/src/pipes/date/date.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ describe('Pipe: _date', () => {

[
{ date, result: `2017-10-17 15:35` },
{ date: +date, result: `2017-10-17 15:35` },
{ date: (+date).toString(), result: `2017-10-17 15:35` },
{ date, result: `2017年10月17日`, format: 'YYYY年MM月DD日' },
{ date: null, result: `` },
{ date: undefined, result: `` },
{ date, result: ``, format: 'fn' },
].forEach((item: any) => {
it(`${'' + item.date} muse be ${item.result}${
it(`${typeof item.date}:${'' + item.date} muse be ${item.result}${
item.format ? `(format: ${item.format})` : ''
}`, () => {
fixture.componentInstance.value = item.date;
Expand Down
6 changes: 3 additions & 3 deletions packages/theme/src/pipes/date/date.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import { Pipe, PipeTransform } from '@angular/core';
import format from 'date-fns/format';
import distanceInWordsToNow from 'date-fns/distance_in_words_to_now';

/**
* @see https://ng-alain.com/docs/service-pipe#%E6%97%A5%E6%9C%9F-_date
*/
@Pipe({ name: '_date' })
export class DatePipe implements PipeTransform {
transform(
Expand All @@ -17,6 +14,9 @@ export class DatePipe implements PipeTransform {
locale: (window as any).__locale__,
});
}
if (typeof value === 'string' && !isNaN(+value)) {
value = +value;
}
return format(value, formatString);
} else {
return '';
Expand Down

0 comments on commit 4c374ad

Please sign in to comment.