Skip to content

Commit

Permalink
fix(datepicker): fix timezone issue (#5364)
Browse files Browse the repository at this point in the history
  • Loading branch information
Domainv authored Sep 9, 2019
1 parent fab160e commit 137042c
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 23 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ install:
- if [[ "$NGV" == "next" ]]; then ./scripts/ci/npm-ng-next.sh; fi
- npm i ngx-bootstrap-ci@$TRAVIS_COMMIT
- npm run ci:rename-pkg
- if [[ "$NGV" == "ivy" ]]; then scripts/ivy/compiler-cli-fix.sh; fi
- if [[ "$TRAVIS_PULL_REQUEST" != false ]]; then export SAUCE_USERNAME=$SAUCE_USERNAME_PR; export SAUCE_ACCESS_KEY=$SAUCE_ACCESS_KEY_PR; export NPM_AUTH_TOKEN_CI=$NPM_AUTH_TOKEN_CI_PR; fi

jobs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import { FormControl, FormGroup } from '@angular/forms';
templateUrl: './custom-format.html'
})
export class DemoDatePickerCustomFormatComponent {
currentDate = new Date();

form = new FormGroup({
dateYMD: new FormControl(new Date()),
dateFull: new FormControl(new Date()),
dateMDY: new FormControl(new Date()),
dateRange: new FormControl([new Date(), new Date()])
dateRange: new FormControl([
new Date(),
new Date(this.currentDate.setDate(this.currentDate.getDate() + 7))
])
});
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"disable-lint": "tslint \"**/*.ts\" -c tslint.json --fix --type-check -t prose -e \"node_modules/**\" -e \"dist/**\" -e \"temp/**\" -e \"scripts/docs/**\"",
"flow.changelog": "conventional-changelog -i CHANGELOG.md -s -p angular",
"flow.github-release": "conventional-github-releaser -p angular",
"build": "run-s build.modules build:schematics build.sass dist-to-modules compiler-cli:fix",
"compiler-cli:fix": "scripts/ivy/compiler-cli-fix.sh",
"build": "run-s build.modules build:schematics build.sass dist-to-modules",
"dist-to-modules": "cp -R ./dist/. ./node_modules/ngx-bootstrap",
"build.watch": "node scripts/build-modules --watch",
"build:schematics": "node scripts/schematics/build",
Expand Down
9 changes: 0 additions & 9 deletions scripts/ivy/compiler-cli-fix.sh

This file was deleted.

17 changes: 16 additions & 1 deletion src/chronos/create/local.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createLocalOrUTC } from './from-anything';
import { DateArray, DateObject } from '../types';
import { DateInput } from '../test/chain';
import { isDate } from '../utils/type-checks';

Expand All @@ -13,3 +12,19 @@ export function parseDate(input: DateInput, format?: string | string[],

return config._d;
}

export function utcAsLocal(date) {
if (!(date instanceof Date)) {
return null;
}

return new Date(
date.getUTCFullYear(),
date.getUTCMonth(),
date.getUTCDate(),
date.getUTCHours(),
date.getUTCMinutes(),
date.getUTCSeconds(),
date.getUTCMilliseconds()
);
}
1 change: 1 addition & 0 deletions src/chronos/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ getMonth
} from './utils/date-getters';

export { parseDate } from './create/local';
export { utcAsLocal } from './create/local';
export { formatDate } from './format';


Expand Down
14 changes: 10 additions & 4 deletions src/datepicker/bs-datepicker-input.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import {
} from '@angular/forms';

import {
parseDate,
formatDate,
getLocale,
isAfter,
isBefore,
isDate,
isDateValid
isDateValid,
parseDate,
utcAsLocal
} from 'ngx-bootstrap/chronos';

import { BsDatepickerDirective } from './bs-datepicker.component';
Expand Down Expand Up @@ -92,7 +93,7 @@ export class BsDatepickerInputDirective

onChange(event: Event) {
/* tslint:disable-next-line: no-any*/
this.writeValue((event.target as any).value);
this.writeValue((event.target as any).value, false);
this._onChange(this._value);
this._onTouched();
}
Expand Down Expand Up @@ -125,7 +126,7 @@ export class BsDatepickerInputDirective
this._validatorChange = fn;
}

writeValue(value: Date | string) {
writeValue(value: Date | string, isUtc = true) {
if (!value) {
this._value = null;
} else {
Expand All @@ -136,7 +137,12 @@ export class BsDatepickerInputDirective
`Locale "${_localeKey}" is not defined, please add it with "defineLocale(...)"`
);
}

this._value = parseDate(value, this._picker._config.dateInputFormat, this._localeService.currentLocale);

if (isUtc) {
this._value = utcAsLocal(this._value);
}
}

this._picker.bsValue = this._value;
Expand Down
30 changes: 25 additions & 5 deletions src/datepicker/bs-daterangepicker-input.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Provider,
Renderer2
} from '@angular/core';

import {
AbstractControl,
ControlValueAccessor,
Expand All @@ -15,7 +16,18 @@ import {
ValidationErrors,
Validator
} from '@angular/forms';
import { parseDate, formatDate, getLocale, isAfter, isBefore, isArray, isDateValid } from 'ngx-bootstrap/chronos';

import {
parseDate,
formatDate,
getLocale,
isAfter,
isBefore,
isArray,
isDateValid,
utcAsLocal
} from 'ngx-bootstrap/chronos';

import { BsDaterangepickerDirective } from './bs-daterangepicker.component';
import { BsLocaleService } from './bs-locale.service';

Expand Down Expand Up @@ -94,7 +106,7 @@ export class BsDaterangepickerInputDirective

onChange(event: Event) {
/* tslint:disable-next-line: no-any*/
this.writeValue((event.target as any).value);
this.writeValue((event.target as any).value, false);
this._onChange(this._value);
this._onTouched();
}
Expand Down Expand Up @@ -130,7 +142,7 @@ export class BsDaterangepickerInputDirective
this._validatorChange = fn;
}

writeValue(value: Date[] | string) {
writeValue(value: Date[] | string, isUtc = true) {
if (!value) {
this._value = null;
} else {
Expand All @@ -153,8 +165,16 @@ export class BsDaterangepickerInputDirective


this._value = (_input as string[])
.map((_val: string): Date =>
parseDate(_val, this._picker._config.dateInputFormat, this._localeService.currentLocale))
.map((_val: string): Date => {
if (isUtc) {
return utcAsLocal(
parseDate(_val, this._picker._config.dateInputFormat, this._localeService.currentLocale)
);
}

return parseDate(_val, this._picker._config.dateInputFormat, this._localeService.currentLocale);
}
)
.map((date: Date) => (isNaN(date.valueOf()) ? null : date));
}

Expand Down

0 comments on commit 137042c

Please sign in to comment.