Harmeet Kohli
Harmeet Kohli

Reputation: 53

Using Angular date filter how to get day of week from date in MM/DD/YYYY format?

Need to get Day i.e. Mon, Tues etc from given date (4/25/2017) which is in MM/DD/YYYY format without using custom filter.

<!DOCTYPE html>
    <html>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
        <body>
        
            <div ng-app="myApp" ng-controller="datCtrl">
                <p>Date = {{ today | date:'EEE' }}</p>
            </div>
        
            <script>
                var app = angular.module('myApp', []);
                app.controller('datCtrl', function($scope) {
                    $scope.today = '4/25/2017'
                });
            </script>
        
            <p>Need to get Day i.e Mon,Tues etc from above date which is in MM/DD/YYYY format without using custom filter. </p>
        
        </body>
    </html>

Upvotes: 3

Views: 16562

Answers (3)

Love-Kesh
Love-Kesh

Reputation: 812

Use getDay function of date

 <!DOCTYPE html>
    <html>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
        <body>

            <div ng-app="myApp" ng-controller="datCtrl">
                 <p>Date = {{ today | date:'EEE'}}</p>
                  <p>Day = {{ day }}</p>
            </div>

            <p>Need to get Day i.e Mon,Tues etc from above date which is in MM/DD/YYYY format without using custom filter. </p>

        </body>
    </html>

Controller is :

   var app = angular.module('myApp', []);
    app.controller('datCtrl', function($scope) {
     $scope.today = new Date('4/25/2017')

    var weekday = [];
    weekday[0] =  "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Tuesday";
    weekday[3] = "Wednesday";
    weekday[4] = "Thursday";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";

    // getDay will return 0,2,3..6
    $scope.day = weekday[$scope.today.getDay()]; 

    });

Upvotes: 2

Sajal
Sajal

Reputation: 4401

You need to make a date object of the date string as:

var app = angular.module('myApp', []);
app.controller('datCtrl', function($scope) {
    $scope.today = new Date('4/25/2017')
});
<!DOCTYPE html>
    <html>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
        <body>
        
            <div ng-app="myApp" ng-controller="datCtrl">
                <p>Date = {{ today | date:'EEE'}}</p>
            </div>

            <p>Need to get Day i.e Mon,Tues etc from above date which is in MM/DD/YYYY format without using custom filter. </p>
        
        </body>
    </html>

Upvotes: 4

Khai Kiong
Khai Kiong

Reputation: 461

you can get this done by using {{ today | date:'mm/dd/yyyy' }}

Upvotes: -3

Related Questions