如何检查日期字符串是否包含小时/分钟/秒零件?

发布于 2025-01-27 15:28:49 字数 232 浏览 3 评论 0原文

我有日期:21-05-2022的JS字符串。也可以将日期字符串作为21-05-2022T23:15:56Z - 带日期和时间数据的日期字符串。我想检查输入字符串是否仅包含日期或时间和时间。现在,我正在使用一个将字符串按't'符号拆分的函数,并检查结果数组是否具有第二个[1]成员。也许有更优雅的方法可以执行此操作?我可以在此上下文中使用moment.js库。

I have JS string with date: 21-05-2022. It is also possible to receive date string as 21-05-2022T23:15:56Z - date string with date and time data. I would like to check if incoming string contains only date or date and time. Now I am using a function which splits string by 'T' symbol and checks if resulted array has second [1] member. Maybe there is more elegant way to perform this operation? I can use moment.js library in this context.

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

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

发布评论

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

评论(2

半山落雨半山空 2025-02-03 15:28:49

Alt变体:
让ISTimeHas =/:/。test(datestring)

Alt variant:
let isTimeHas = /:/.test(dateString)

再见回来 2025-02-03 15:28:49

由于您的日期始终是字符串,因此我会做:
让结果= datestring.includes('t');如果字符串包含t,则表示它是DateTime

since your date is always string, i would do :
let result = dateString.includes('T'); if the string includes the T it means it is datetime

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