最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - getTime() in AngularJS , error in new Date(date) in Java Script - Stack Overflow

programmeradmin2浏览0评论
$scope.notAvailableDayClick=function(val1,date){    
        console.log("day clcik")
        var startDate=$filter('date')(date,'yyyy-MM-dd')
        var endDate=new Date(startDate)
        endDate.setMinutes(59)
        endDate.setHours(23)
    }

date is 2015-01-16 if I do this

new Date(date)

Thu Jan 15 2015 16:00:00 GMT-0800 (Pacific Standard Time)

So I have to go with AngularJS

var startDate=$filter('date')(date,'yyyy-MM-dd')

but now I need startDate.getTime(), error occur I think it takes it as a String

$scope.notAvailableDayClick=function(val1,date){    
        console.log("day clcik")
        var startDate=$filter('date')(date,'yyyy-MM-dd')
        var endDate=new Date(startDate)
        endDate.setMinutes(59)
        endDate.setHours(23)
    }

date is 2015-01-16 if I do this

new Date(date)

Thu Jan 15 2015 16:00:00 GMT-0800 (Pacific Standard Time)

So I have to go with AngularJS

var startDate=$filter('date')(date,'yyyy-MM-dd')

but now I need startDate.getTime(), error occur I think it takes it as a String

Share Improve this question edited Jan 4, 2015 at 18:35 simeg 1,8792 gold badges27 silver badges34 bronze badges asked Jan 4, 2015 at 18:32 abhaygarg12493abhaygarg12493 1,6052 gold badges21 silver badges43 bronze badges 1
  • 1 Date constructor expects string parameter in the following form: MM-dd-yyyy. yyyy-MM-dd form is unacceptable. – hindmost Commented Jan 4, 2015 at 18:55
Add a ment  | 

3 Answers 3

Reset to default 3

As per angular docs the filter returns a String in requested format. Date constructor accepts ISO8601 formats usually although some browsers support many formats as I remember. Probably your format yy-MM-dd is not supported.

I hope the variable date is a valid Date object, in that case why don't you use it instead of the formatted string you made with angular filter?

var endDate = new Date(date);
endDate.setMinutes(59);
endDate.setHours(23);

Also you have a Date constructor that accepts the format

new Date(year, month[, date[, hour[, minutes[, seconds[, milliseconds]]]]]);

So if what you have in hand is 2015-01-16 you can get midnight of that day with:

var startDate = "2015-01-16";
var year = parseInt(startDate.split('-')[0], 10);
var month = parseInt(startDate.split('-')[1], 10) - 1;
var year = parseInt(startDate.split('-')[2], 10);
var endDate = new Date(year, month, date, 23, 59);

Just use the original date to create endDate not the angular filtered version

var endDate=new Date(date);
endDate.setMinutes(59);
endDate.setHours(23);

Best option is to use ISO-String, because Google Chrome supports this format: MM-dd-yyyy. In Mozilla this format gives Invalid Date.

new Date('MM-dd-yyyy')

So using Iso-String in Angular, it can done as follows:

new Date($filter('date')(yourdDate,'yyyy-MM-ddTHH:mm:ss.sssZ'))
发布评论

评论列表(0)

  1. 暂无评论