Skip to content

Commit 137042c

Browse files
authored
fix(datepicker): fix timezone issue (#5364)
1 parent fab160e commit 137042c

File tree

8 files changed

+58
-23
lines changed

8 files changed

+58
-23
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ install:
5858
- if [[ "$NGV" == "next" ]]; then ./scripts/ci/npm-ng-next.sh; fi
5959
- npm i ngx-bootstrap-ci@$TRAVIS_COMMIT
6060
- npm run ci:rename-pkg
61-
- if [[ "$NGV" == "ivy" ]]; then scripts/ivy/compiler-cli-fix.sh; fi
6261
- 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
6362

6463
jobs:

demo/src/app/components/+datepicker/demos/custom-format/custom-format.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ import { FormControl, FormGroup } from '@angular/forms';
66
templateUrl: './custom-format.html'
77
})
88
export class DemoDatePickerCustomFormatComponent {
9+
currentDate = new Date();
910

1011
form = new FormGroup({
1112
dateYMD: new FormControl(new Date()),
1213
dateFull: new FormControl(new Date()),
1314
dateMDY: new FormControl(new Date()),
14-
dateRange: new FormControl([new Date(), new Date()])
15+
dateRange: new FormControl([
16+
new Date(),
17+
new Date(this.currentDate.setDate(this.currentDate.getDate() + 7))
18+
])
1519
});
1620
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
"disable-lint": "tslint \"**/*.ts\" -c tslint.json --fix --type-check -t prose -e \"node_modules/**\" -e \"dist/**\" -e \"temp/**\" -e \"scripts/docs/**\"",
2525
"flow.changelog": "conventional-changelog -i CHANGELOG.md -s -p angular",
2626
"flow.github-release": "conventional-github-releaser -p angular",
27-
"build": "run-s build.modules build:schematics build.sass dist-to-modules compiler-cli:fix",
28-
"compiler-cli:fix": "scripts/ivy/compiler-cli-fix.sh",
27+
"build": "run-s build.modules build:schematics build.sass dist-to-modules",
2928
"dist-to-modules": "cp -R ./dist/. ./node_modules/ngx-bootstrap",
3029
"build.watch": "node scripts/build-modules --watch",
3130
"build:schematics": "node scripts/schematics/build",

scripts/ivy/compiler-cli-fix.sh

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/chronos/create/local.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { createLocalOrUTC } from './from-anything';
2-
import { DateArray, DateObject } from '../types';
32
import { DateInput } from '../test/chain';
43
import { isDate } from '../utils/type-checks';
54

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

1413
return config._d;
1514
}
15+
16+
export function utcAsLocal(date) {
17+
if (!(date instanceof Date)) {
18+
return null;
19+
}
20+
21+
return new Date(
22+
date.getUTCFullYear(),
23+
date.getUTCMonth(),
24+
date.getUTCDate(),
25+
date.getUTCHours(),
26+
date.getUTCMinutes(),
27+
date.getUTCSeconds(),
28+
date.getUTCMilliseconds()
29+
);
30+
}

src/chronos/public_api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ getMonth
1414
} from './utils/date-getters';
1515

1616
export { parseDate } from './create/local';
17+
export { utcAsLocal } from './create/local';
1718
export { formatDate } from './format';
1819

1920

src/datepicker/bs-datepicker-input.directive.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ import {
1818
} from '@angular/forms';
1919

2020
import {
21-
parseDate,
2221
formatDate,
2322
getLocale,
2423
isAfter,
2524
isBefore,
2625
isDate,
27-
isDateValid
26+
isDateValid,
27+
parseDate,
28+
utcAsLocal
2829
} from 'ngx-bootstrap/chronos';
2930

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

9394
onChange(event: Event) {
9495
/* tslint:disable-next-line: no-any*/
95-
this.writeValue((event.target as any).value);
96+
this.writeValue((event.target as any).value, false);
9697
this._onChange(this._value);
9798
this._onTouched();
9899
}
@@ -125,7 +126,7 @@ export class BsDatepickerInputDirective
125126
this._validatorChange = fn;
126127
}
127128

128-
writeValue(value: Date | string) {
129+
writeValue(value: Date | string, isUtc = true) {
129130
if (!value) {
130131
this._value = null;
131132
} else {
@@ -136,7 +137,12 @@ export class BsDatepickerInputDirective
136137
`Locale "${_localeKey}" is not defined, please add it with "defineLocale(...)"`
137138
);
138139
}
140+
139141
this._value = parseDate(value, this._picker._config.dateInputFormat, this._localeService.currentLocale);
142+
143+
if (isUtc) {
144+
this._value = utcAsLocal(this._value);
145+
}
140146
}
141147

142148
this._picker.bsValue = this._value;

src/datepicker/bs-daterangepicker-input.directive.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
Provider,
88
Renderer2
99
} from '@angular/core';
10+
1011
import {
1112
AbstractControl,
1213
ControlValueAccessor,
@@ -15,7 +16,18 @@ import {
1516
ValidationErrors,
1617
Validator
1718
} from '@angular/forms';
18-
import { parseDate, formatDate, getLocale, isAfter, isBefore, isArray, isDateValid } from 'ngx-bootstrap/chronos';
19+
20+
import {
21+
parseDate,
22+
formatDate,
23+
getLocale,
24+
isAfter,
25+
isBefore,
26+
isArray,
27+
isDateValid,
28+
utcAsLocal
29+
} from 'ngx-bootstrap/chronos';
30+
1931
import { BsDaterangepickerDirective } from './bs-daterangepicker.component';
2032
import { BsLocaleService } from './bs-locale.service';
2133

@@ -94,7 +106,7 @@ export class BsDaterangepickerInputDirective
94106

95107
onChange(event: Event) {
96108
/* tslint:disable-next-line: no-any*/
97-
this.writeValue((event.target as any).value);
109+
this.writeValue((event.target as any).value, false);
98110
this._onChange(this._value);
99111
this._onTouched();
100112
}
@@ -130,7 +142,7 @@ export class BsDaterangepickerInputDirective
130142
this._validatorChange = fn;
131143
}
132144

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

154166

155167
this._value = (_input as string[])
156-
.map((_val: string): Date =>
157-
parseDate(_val, this._picker._config.dateInputFormat, this._localeService.currentLocale))
168+
.map((_val: string): Date => {
169+
if (isUtc) {
170+
return utcAsLocal(
171+
parseDate(_val, this._picker._config.dateInputFormat, this._localeService.currentLocale)
172+
);
173+
}
174+
175+
return parseDate(_val, this._picker._config.dateInputFormat, this._localeService.currentLocale);
176+
}
177+
)
158178
.map((date: Date) => (isNaN(date.valueOf()) ? null : date));
159179
}
160180

0 commit comments

Comments
 (0)