英文:
Creating a date object from a date string is one day older
问题
我有一个函数,其中我将日期字符串或日期时间字符串转换为时间设置为00:00:00
的日期对象 - 基本上是截断时间。
/**
* @param dateString - 可以是 2023-07-13 或 2023-07-13 03:03:03
*/
const removeTime = (dateString: string): Date | undefined => {
if (isValid(new Date(dateString))) {
const timestamp = new Date(dateString);
console.log('PARAM', dateString);
console.log('NATIVE', timestamp);
console.log('BEFORE TRUNCATE', format(timestamp, Constants.FORMAT_DATE));
console.log(
'AFTER TRUNCATE',
format(
new Date(timestamp.getFullYear(), timestamp.getMonth(), timestamp.getDate()),
Constants.FORMAT_DATE
)
);
return new Date(timestamp.getFullYear(), timestamp.getMonth(), timestamp.getDate());
} else {
return undefined;
}
};
请注意,isValid
和 format
调用都来自于 date-fns。
我的问题是,new Date(dateString)
调用会将日期向前设置一天。
PARAM: 2023-07-13
NATIVE: Wed Jul 12 2023 18:00:00 GMT-0600 (Mountain Daylight Time)
BEFORE TRUNCATE: 2023-07-12
AFTER TRUNCATE: 2023-07-12
我的 Constants.FORMAT_DATE
是 yyyy-MM-dd
。
附加信息
看起来只有当 dateString
没有时间部分时(即 2023-07-13
)才会出现此问题。如果字符串包含时间(即 2023-07-13 03:03:03
),则函数实际上可以正常工作。
PARAM: 2023-07-13 11:26:11
NATIVE: Thu Jul 13 2023 11:26:11 GMT-0600 (Mountain Daylight Time)
BEFORE TRUNCATE: 2023-07-13
AFTER TRUNCATE: 2023-07-13
英文:
I have a function where I convert a date string or a datetime string into a date object with time set to 00:00:00
- basically truncating the time.
/**
* @param dateString - can either be 2023-07-13 or 2023-07-13 03:03:03
*/
const removeTime = (dateString: string): Date | undefined => {
if (isValid(new Date(dateString))) {
const timestamp = new Date(dateString);
console.log('PARAM', dateString);
console.log('NATIVE', timestamp);
console.log('BEFORE TRUNCATE', format(timestamp, Constants.FORMAT_DATE));
console.log(
'AFTER TRUNCATE',
format(
new Date(timestamp.getFullYear(), timestamp.getMonth(), timestamp.getDate()),
Constants.FORMAT_DATE
)
);
return new Date(timestamp.getFullYear(), timestamp.getMonth(), timestamp.getDate());
} else {
return undefined;
}
};
Please note that both the isValid
and format
calls are coming in from date-fns.
My problem is that the new Date(dateString)
call sets the day back by 1.
PARAM: 2023-07-13
NATIVE: Wed Jul 12 2023 18:00:00 GMT-0600 (Mountain Daylight Time)
BEFORE TRUNCATE: 2023-07-12
AFTER TRUNCATE: 2023-07-12
My Constants.FORMAT_DATE
is yyyy-MM-dd
.
ADDITIONAL INFO
It looks like this is only an issue if the dateString
doesn't have a time part to it (i.e. 2023-07-13
). The function actually works just fine if the string contains a time (i.e. 2023-07-13 03:03:03
).
PARAM: 2023-07-13 11:26:11
NATIVE: Thu Jul 13 2023 11:26:11 GMT-0600 (Mountain Daylight Time)
BEFORE TRUNCATE: 2023-07-13
AFTER TRUNCATE: 2023-07-13
答案1
得分: 1
你应该添加时区偏移:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const isValid = dt => !isNaN(+dt);
const removeTime = (dateString) => {
const dt = new Date(dateString);
if (!isValid(dt )) {
return;
}
dt.setMinutes(dt.getMinutes() + dt.getTimezoneOffset());
return new Date(dt.getFullYear(), dt.getMonth(), dt.getDate());
};
console.log(removeTime('2023-07-13').toString());
console.log(removeTime('2023-07-13 03:03:03').toString());
<!-- end snippet -->
英文:
You should add the timezone offset:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const isValid = dt => !isNaN(+dt);
const removeTime = (dateString) => {
const dt = new Date(dateString);
if (!isValid(dt )) {
return;
}
dt.setMinutes(dt.getMinutes() + dt.getTimezoneOffset());
return new Date(dt.getFullYear(), dt.getMonth(), dt.getDate());
};
console.log(removeTime('2023-07-13').toString());
console.log(removeTime('2023-07-13 03:03:03').toString());
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论