如何不将数字作为日期的字符串解析?
我想格式化一个日期字符串,但对于所有其他字符串,只需返回该输入即可。 现在,当输入是包含任何数字的字符串时,例如“您是数字1”时,字符串被解析为有效日期“ 2001年1月1日”。
const value = new Date(input);
if (!(value instanceof Date && isFinite(value as unknown as number))) {
return input;
}
// format date and return
如何检查该字符串是否真的是仅包含数字或日期为字符串的字符串? 输入日期字符串可能始终具有相同的模式,但理想情况下,这无关紧要。
I want to format a date string but for all other strings just return that input.
Now when the input is a string that contains any number e.g. "You are number 1", the string is parsed as a valid date "1 Jan 2001".
const value = new Date(input);
if (!(value instanceof Date && isFinite(value as unknown as number))) {
return input;
}
// format date and return
How can I check if that string is really a string containing just a number or a date as string?
The input date string might always have the same pattern but ideally it shouldn't matter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对解决方案并不满意,但到目前为止运行良好。
为此测试:
Not that happy with the solution but it is working fine so far.
Test for that: