Sephen
Sephen

Reputation: 1111

AngularJs day of the week - number in ng-repeat

How do i get the day of the week in angular.

eg. sunday = 00, monday = 01 etc.

I know about the date filter: (https://docs.angularjs.org/api/ng/filter/date) but the week day number apparently doesn't exist.

The problem that the date comes from a post date in ng-repeat

<div ng-repeat="post in posts">
   <span class="daycontainer day{{post.date | date: (daynumberformat) }}">
</div>

I know there is a javascript function getDay() but i do not get it to work

<div ng-repeat="post in posts"> 
    <script type="text/javascript">
        var d = new Date({{post.date}});
        var dn = d.getDay();
        document.getElementById("{{post.id}}").className += " day" + dn;
    </script>
    <span id="{{post.id}}" class="daycontainer">{{post.date | date : "EEEE, MMMM d, y"}}</span>
</div>

the result is:

<span id="1-posttitle" class="daycontainer dayNaN ng-binding dayNaN dayNaN dayNaN dayNaN dayNaN dayNaN dayNaN dayNaN dayNaN dayNaN dayNaN">friday, february 26, 2016</span>

Upvotes: 1

Views: 2206

Answers (1)

Eria
Eria

Reputation: 3192

As @charlietfl said, you can use javascript Date object :

http://www.w3schools.com/jsref/jsref_obj_date.asp

For example, using getDay() function :

var day = (new Date()).getDay();

All the link in the world, to please raging people :

Upvotes: 1

Related Questions