abhaygarg12493
abhaygarg12493

Reputation: 1605

getTime() in AngularJS , error in new Date(date) in Java Script

$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

Upvotes: 1

Views: 7813

Answers (3)

abhaygarg12493
abhaygarg12493

Reputation: 1605

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'))

Upvotes: 2

sabithpocker
sabithpocker

Reputation: 15558

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);

Upvotes: 3

charlietfl
charlietfl

Reputation: 171679

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

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

Upvotes: 2

Related Questions