如何区分时间字符串和非时间字符串

发布于 2024-11-29 07:58:42 字数 363 浏览 1 评论 0原文

我需要一个函数来判断字符串是否采用时间格式。沿着这些思路:

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 技术交流群。

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

发布评论

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

评论(3

浮世清欢 2024-12-06 07:58:43
var string1 = "Normal String",
    string2 = "12:00 pm";


function timeOrString(str){
  if(new Date("1/1/1900 " + str) != "Invalid Date" && str){
    alert("this is a valid time string");
  }else{
    alert("this is not a valid time string");
  }
}

timeOrString(string1);
timeOrString(string2);

这是一种方法。这很混乱,但在大多数情况下它会起作用。 :P

var string1 = "Normal String",
    string2 = "12:00 pm";


function timeOrString(str){
  if(new Date("1/1/1900 " + str) != "Invalid Date" && str){
    alert("this is a valid time string");
  }else{
    alert("this is not a valid time string");
  }
}

timeOrString(string1);
timeOrString(string2);

That's one way of doing it. It's messy but it'll work... most of the time. :P

萌面超妹 2024-12-06 07:58:43

有几种方法可以做到这一点...有些使用正则表达式其他值检查...以下是一些示例:

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/

書生途 2024-12-06 07:58:43

您可以使用instanceof和typeof运算符

var theDay = new Date(1995, 11, 17); // Dec. 17, 1995
 if (theDay instanceof Date)
 {
  print("theDay is a Date object");
   // whatever else...
 }

var myString = new String();
var myDate = new Date();


    myString instanceof String; // returns true
    myString instanceof Object; // returns true
    myString instanceof Date;   // returns false
    myDate instanceof Date;     // returns true
    myDate instanceof Object;   // returns true
    myDate instanceof String;   // returns false

instanceof

typeof

You can use instanceof and typeof operators

var theDay = new Date(1995, 11, 17); // Dec. 17, 1995
 if (theDay instanceof Date)
 {
  print("theDay is a Date object");
   // whatever else...
 }

var myString = new String();
var myDate = new Date();


    myString instanceof String; // returns true
    myString instanceof Object; // returns true
    myString instanceof Date;   // returns false
    myDate instanceof Date;     // returns true
    myDate instanceof Object;   // returns true
    myDate instanceof String;   // returns false

instanceof

typeof

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