1
$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

1
  • 1
    Date constructor expects string parameter in the following form: MM-dd-yyyy. yyyy-MM-dd form is unacceptable. Commented Jan 4, 2015 at 18:55

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

2

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

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

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.