fix(datetime): date strings no longer revert to previous day (#18018)

fixes #17977
This commit is contained in:
Liam DeBeasi
2019-04-16 12:34:35 -04:00
committed by GitHub
parent 447497427e
commit cc60b60135
2 changed files with 30 additions and 2 deletions

View File

@ -248,8 +248,20 @@ export function parseDate(val: string | undefined | null): DatetimeData | undefi
* such as "01:47"
*/
export const getLocalDateTime = (dateString: any = ''): Date => {
const date = (typeof dateString === 'string' && dateString.length > 0) ? new Date(dateString) : new Date();
/**
* Ensures that YYYY-MM-DD, YYYY-MM,
* YYYY-DD, etc does not get affected
* by timezones and stays on the day/month
* that the user provided
*/
if (
dateString.length === 10 ||
dateString.length === 7
) {
dateString += ' ';
}
const date = (typeof dateString === 'string' && dateString.length > 0) ? new Date(dateString) : new Date();
return new Date(
Date.UTC(
date.getFullYear(),
@ -267,7 +279,6 @@ export function updateDate(existingData: DatetimeData, newData: any): boolean {
if (!newData || typeof newData === 'string') {
const localDateTime = getLocalDateTime(newData);
if (!Number.isNaN(localDateTime.getTime())) {
newData = localDateTime.toISOString();
}