1

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>
Sephen
  • 1,111
  • 3
  • 16
  • 38
  • Angular is javscript so use javascript Date methods – charlietfl Feb 23 '16 at 14:58
  • `EEEE` gives you the string/name of the day of the week, but you need the number day of the week(0-6), right? – tenor528 Feb 23 '16 at 15:13
  • The problem is that the date comes from a ng-repeat post date – Sephen Feb 23 '16 at 15:26
  • If *post.date* is not parsable by the Date constructor, then `new Date({{post.date}})` will return a Date object whose *timevalue* is NaN and calling any Date method like *getDay* will return NaN too. What is the actual value of *post.date*? Note that parsing strings with the Date constructor is well known to be unreliable and strongly advised against. – RobG Feb 24 '16 at 02:29

1 Answers1

1

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 :

Eria
  • 2,653
  • 6
  • 29
  • 56
  • 2
    Please don't reference w3schools, ECMA-262 or MDN are far better resources. And the OP is already using *getDay*, the issue is elsewhere. – RobG Feb 24 '16 at 02:26
  • As you can see, the initial post has been edited 15 hours ago at the moment I'm writing this. My answer was postd 16 hours ago. Do you really think I would propose a solution already present in the question ?! – Eria Feb 24 '16 at 07:44