如何区分时间字符串和非时间字符串
我需要一个函数来判断字符串是否采用时间格式。沿着这些思路:
var string1 = "Normal String",
string2 = "12:00pm";
function timeOrString(str) {
if (str == A TIME) {
alert('this is a time');
}
else {
alert('this is a normal string');
}
}
timeOrString(string1);
timeOrString(string2);
这可能是简单的事情,比如判断字符串中是否有数字?
I need a function that can tell if a string is in a time format. Something along these lines:
var string1 = "Normal String",
string2 = "12:00pm";
function timeOrString(str) {
if (str == A TIME) {
alert('this is a time');
}
else {
alert('this is a normal string');
}
}
timeOrString(string1);
timeOrString(string2);
It could be something as simple as telling whether there is a number in the string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一种方法。这很混乱,但在大多数情况下它会起作用。 :P
That's one way of doing it. It's messy but it'll work... most of the time. :P
有几种方法可以做到这一点...有些使用正则表达式其他值检查...以下是一些示例:
http://javascript.internet.com/forms/val-time.html
http://www.the-art-of-web.com/ javascript/验证日期/
http://www.codeproject.com/KB/scripting/timevalidation.aspx
但我的建议是,根据您想要执行的操作,不要使用文本框来请求时间,而是使用 jquery 插件来选择日期和时间,并以这种方式控制输入格式:
http://trentrichardson.com/examples/timepicker/
There are several ways to do it... Some use Regular Expresions other value checks... Here are some examples:
http://javascript.internet.com/forms/val-time.html
http://www.the-art-of-web.com/javascript/validate-date/
http://www.codeproject.com/KB/scripting/timevalidation.aspx
But my recomendation, depending on what you are trying to do, its not use a text box to request the time, but use a jquery plugin to make date and time selectable, and control the input format that way:
http://trentrichardson.com/examples/timepicker/
您可以使用instanceof和typeof运算符
instanceof
typeof
You can use instanceof and typeof operators
instanceof
typeof