如何检查日期字符串是否包含小时/分钟/秒零件?
我有日期: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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Alt变体:
让ISTimeHas =/:/。test(datestring)
Alt variant:
let isTimeHas = /:/.test(dateString)
由于您的日期始终是字符串,因此我会做:
让结果= datestring.includes('t');
如果字符串包含t,则表示它是DateTimesince your date is always string, i would do :
let result = dateString.includes('T');
if the string includes the T it means it is datetime