解析日期没有时间在React JS中?

发布于 2025-02-01 02:06:37 字数 858 浏览 4 评论 0原文

谁能帮助我使用React日历日期?我正在显示一个我使用的搜索过滤器:

为此,我正在使用React usestate:

const [selectedDate, setSelectedDate] = useState<Date | null >();

<Input label="selected on" type="date" value={startDate} />

现在,选择后,它作为UTC的时间按时间如下:

http://localhost:10000/candidates/projects?page_number=1&page_size=20&selected__date__option=2022-05-25T00:00:00.000Z

我只想从此日期日期,为此我使用以下代码:

 const convert = (str: Date): Date => {
 const date = new Date(str),
       mnth = ("0" + (date.getMonth() + 1)).slice(-2),
       day = ("0" + date.getDate()).slice(-2);
       return  new Date([date.getFullYear(), mnth, day].join("-"));
};

但是返回类型是String,Usestate寻找date类型。 那么,谁能指出我在这里缺少什么?

我想要类型的2022-05-26 date

Can anyone help me with react calendar dates? I am showing a search filter where I am using :

for this I am using react useState :

const [selectedDate, setSelectedDate] = useState<Date | null >();

<Input label="selected on" type="date" value={startDate} />

Now, after selection, it is coming as UTC date with time as follows:

http://localhost:10000/candidates/projects?page_number=1&page_size=20&selected__date__option=2022-05-25T00:00:00.000Z

I want only date from this and for that I am using following code:

 const convert = (str: Date): Date => {
 const date = new Date(str),
       mnth = ("0" + (date.getMonth() + 1)).slice(-2),
       day = ("0" + date.getDate()).slice(-2);
       return  new Date([date.getFullYear(), mnth, day].join("-"));
};

but the return type is string and useState looks for Date type.
So, can anyone point what I am missing here?

I want selected__date__option=2022-05-26 of type Date

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

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

发布评论

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

评论(1

琉璃繁缕 2025-02-08 02:06:37

您可以使用此操作

new Intl.DateTimeFormat('en-US').format(new Date(str))

查看MDN上的此文档=&gt;

You can use this action

new Intl.DateTimeFormat('en-US').format(new Date(str))

Check out this documentation on MDN as well => MDN Link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文