如果传递的参数不是日期对象,则AngularJS:isDate()返回false
我只是尝试检查字符串是否为日期。这是我在angular JS中的代码如果传递的参数不是日期对象,则AngularJS:isDate()返回false,angularjs,Angularjs,我只是尝试检查字符串是否为日期。这是我在angular JS中的代码 var mydt= "2015/07/29"; document.write(angular.isDate(mydt)); 这总是返回false。 但实际上这是一个约会 但是当我尝试这个代码时 var cur_date = new Date(); document.write(cur_date); document.write(angular.isDate(cur_date)); 执行结果是 2015年7月2
var mydt= "2015/07/29";
document.write(angular.isDate(mydt));
这总是返回false。
但实际上这是一个约会
但是当我尝试这个代码时
var cur_date = new Date();
document.write(cur_date);
document.write(angular.isDate(cur_date));
执行结果是
2015年7月29日星期三15:15:13 GMT+0530(斯里兰卡标准时间)
真的
我想知道为什么我们不能以简单的方式检查简单的日期格式,如“yyyy/mm/dd”。angular.isDate()
检查输入是否为日期类型。
来自AngularJS的函数源
function isDate(value) {
return toString.call(value) === '[object Date]';
}
这就是为什么angular.isDate在日期对象上返回true,而在字符串上返回false
var d = new Date('7/29/2015');
alert(angular.isDate(d)); // Alerts true on the date object
alert(d.toString()); // Alerts the string value of the date object
alert(angular.isDate(d.toString())); // Alerts false on the string value
您可以使用Date.parse():
检查字符串是否包含数字4 2 2在0000-00-00上失败
function isValidDate(dateString) {
var regEx = /^\d{4}-\d{2}-\d{2}$/;
return dateString.match(regEx) != null;
}