从日期字符串创建日期对象比原日期早一天。

huangapple go评论82阅读模式
英文:

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;
	}
};

请注意,isValidformat 调用都来自于 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_DATEyyyy-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 =&gt; !isNaN(+dt);

const removeTime = (dateString) =&gt; {
    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(&#39;2023-07-13&#39;).toString());
console.log(removeTime(&#39;2023-07-13 03:03:03&#39;).toString());

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月14日 02:46:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定