javascript - Angular js - isDate() won't work - Stack Overflow

Im trying checking if a value is a date like this:var input = "2014-04-01T15:22:30.000Z";var

Im trying checking if a value is a date like this:

var input = "2014-04-01T15:22:30.000Z";

var dateTime = {
    year: $filter('date')(input,'yyyy'),
    day: $filter('date')(input,'EEE'),
    dayNum: $filter('date')(input,'d'),
    month: $filter('date')(input,'MMM'),
    hours:$filter('date')(input,'H'),
    mins: $filter('date')(input,'m'),
    seconds:$filter('date')(input,'ss')
};

if(angular.isDate(dateTime.year+' '+dateTime.month+' '+dateTime.dayNum)) { 
    // always false 
}

why is always false?

The year,month etc pieces are correctly filtered :/

Im trying checking if a value is a date like this:

var input = "2014-04-01T15:22:30.000Z";

var dateTime = {
    year: $filter('date')(input,'yyyy'),
    day: $filter('date')(input,'EEE'),
    dayNum: $filter('date')(input,'d'),
    month: $filter('date')(input,'MMM'),
    hours:$filter('date')(input,'H'),
    mins: $filter('date')(input,'m'),
    seconds:$filter('date')(input,'ss')
};

if(angular.isDate(dateTime.year+' '+dateTime.month+' '+dateTime.dayNum)) { 
    // always false 
}

why is always false?

The year,month etc pieces are correctly filtered :/

Share Improve this question edited Apr 10, 2014 at 22:08 tshepang 12.5k25 gold badges98 silver badges139 bronze badges asked Apr 8, 2014 at 13:35 Filippo orettiFilippo oretti 49.9k96 gold badges229 silver badges351 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

The following statement is always false, maybe your assumption about how isDate works is incorrect?

angular.isDate('2014 Mar 1');

Here's the source code for the isDate function. You'll see that it checks whether the supplied value is a javascript Date object.

function isDate(value){
  return toString.call(value) === '[object Date]';
}

You might wonder why angular allows you to do the following:

var input = "2014-04-01T15:22:30.000Z";
var year = $filter('date')(input,'yyyy');

It's because the date filter actually converts your input into a date before returning a formatted copy. So, with the date filter, it lets you pass in a string, but the isDate function requires a Date object.

dateTime.year+' '+dateTime.month+' '+dateTime.dayNum is a string, not a date object.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745330852a4622869.html

相关推荐

  • javascript - Angular js - isDate() won't work - Stack Overflow

    Im trying checking if a value is a date like this:var input = "2014-04-01T15:22:30.000Z";var

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信