JS日期格式给出-1天

发布于 2025-01-18 15:47:03 字数 442 浏览 0 评论 0原文

我想使用 toIsoString 函数格式化日期,但该函数返回 -1 天如何修复? event.value 格式为 Tue Apr 19 2022 00:00:00 GMT+0400 (亚美尼亚标准时间)

 console.log(new Date(event.value).toISOString().slice(0,10))

控制台中的 ,我得到的是这个 2022-04-18代码> 如何查看结果为 -1 天

  startDateChange(event: any): void{
    this.firstDayOfMonth = event;
    console.log(new Date(event.value).toISOString().slice(0, 10));
  }

I want to format my date with toIsoString function but the function returns -1 day how to fix it? the event.value format is Tue Apr 19 2022 00:00:00 GMT+0400 (Armenia Standard Time)

 console.log(new Date(event.value).toISOString().slice(0,10))

in the console, I'm getting this one 2022-04-18 how you can see the result was -1 day

  startDateChange(event: any): void{
    this.firstDayOfMonth = event;
    console.log(new Date(event.value).toISOString().slice(0, 10));
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难如初 2025-01-25 15:47:03

这是因为您的时区设置。当您在时区使用 +0400 时,toISOString 始终返回 +0000。这实际上减少了 4 个小时,相当于前一天的 20:00。

您可以尝试通过向日期添加 Z 将时区偏移设置为 0 来解决此问题,如第二个示例所示。

第三个示例是一种安全的方法,但如果需要,您需要添加前缀 0。

另一种选择是使用像 Moment.js 这样的日期库,尽管 moment 已被弃用(我不确定最好的选择是什么,这取决于你)。

// Your example (this might work for some people that are in the +0000 timezone)
console.log(new Date('2021-03-03T00:00:00').toISOString().slice(0,10));

// Second example
console.log(new Date('2021-03-03T00:00:00z').toISOString().slice(0,10));

// Third example
const date = new Date('2021-03-03T00:00:00');
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();

console.log(`${year}-${month}-${day}`);

This is because of your timezone settings. The toISOString is always returning +0000 while you work with +0400 in your timezone. This essentially withdraws 4 hours resulting in the day before, 20:00 hours.

You could try to fix this by setting the timezone offset to 0 by adding a Z to the date as shown in the second example.

The third example is a safe way to do it but you need to prefix the 0 if you need that.

Another alternative would be to use a date-library like Moment.js, although moment is deprecated (I don't know for sure what the best alternative would be, that's up to you).

// Your example (this might work for some people that are in the +0000 timezone)
console.log(new Date('2021-03-03T00:00:00').toISOString().slice(0,10));

// Second example
console.log(new Date('2021-03-03T00:00:00z').toISOString().slice(0,10));

// Third example
const date = new Date('2021-03-03T00:00:00');
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();

console.log(`${year}-${month}-${day}`);

为你拒绝所有暧昧 2025-01-25 15:47:03

您可以使用 https://day.js.org/

代码:

const dateString = 'Tue Apr 19 2022 00:00:00 GMT+0400 (Armenia Standard Time)'

const date1 = dayjs(dateString).format('YYYY-MM-DD')
console.log(date1)

const date2 = dayjs(dateString).format('YYYY-M-D')
console.log(date2)
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.0/dayjs.min.js"></script>

You can use https://day.js.org/

Code:

const dateString = 'Tue Apr 19 2022 00:00:00 GMT+0400 (Armenia Standard Time)'

const date1 = dayjs(dateString).format('YYYY-MM-DD')
console.log(date1)

const date2 = dayjs(dateString).format('YYYY-M-D')
console.log(date2)
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.0/dayjs.min.js"></script>

你的背包 2025-01-25 15:47:03

就我而言,我通过使用 Dayjs 的时区插件解决了这个问题。如果您使用 dayjs,那是因为时区的原因。我用这条线解决了这个问题。

import * as dayjs from 'dayjs';
import * as timezone from 'dayjs/plugin/timezone';
import * as utc from 'dayjs/plugin/utc';
import * as customParseFormat from 'dayjs/plugin/customParseFormat';

dayjs.extend(customParseFormat);
dayjs.extend(utc);
dayjs.extend(timezone);

reviewData.dateComplete = "20-02-2024"
const dateTz = dayjs.tz(reviewData.dateComplete, 'DD-MM-YYYY','UTC').toDate();
console.log(dateTz) // 2024-02-20T00:00:00.000Z

In my case, I solved the issue by using the timezone plugin from Dayjs. If you use dayjs, it's because of the timezone. I solved this with this line.

import * as dayjs from 'dayjs';
import * as timezone from 'dayjs/plugin/timezone';
import * as utc from 'dayjs/plugin/utc';
import * as customParseFormat from 'dayjs/plugin/customParseFormat';

dayjs.extend(customParseFormat);
dayjs.extend(utc);
dayjs.extend(timezone);

reviewData.dateComplete = "20-02-2024"
const dateTz = dayjs.tz(reviewData.dateComplete, 'DD-MM-YYYY','UTC').toDate();
console.log(dateTz) // 2024-02-20T00:00:00.000Z
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文