How to Parse string into Date in angularjs

15,169

Solution 1

Try this in your html :

{{item.startDate | date: 'MMM dd, yyyy'}}

Solution 2

You can use AngularJS filter like below-

$scope.parseDate = function (date) {
    $filter('date')(new Date(Date.parse(date)), 'yyyy-MM-dd');
} 
Share:
15,169
KK SK
Author by

KK SK

Updated on June 06, 2022

Comments

  • KK SK
    KK SK almost 2 years

    I have data from the server to display. I receive strings such as this: 2016-05-01. I need to filter the data between two dates in that format. I have a function to parse the date as it is:

    $scope.parseDate = function (date) {
      return new Date(Date.parse(date));
    } 
    

    In my html I display the date with this:

    {{ parseDate(item.startDate) }}`
    

    But in the browser, the result is: "2016-07-12T00:00:00.000Z".

    1. I need the parse the string into the same format as from the server: 2016-05-01.

    2. I need a simple filter function.

    I don't want to use moment.js. My backend platform is JavaScript.