Date format per year

I have a date coming from an API that returns a date like this: 2012-10-12 00:00:00

I am printing it on my page like this:

<span class="year" ng-bind-html="hit._highlightResult.original_release_date.value"></span>

      

s original_release_date.value

is the date (2012-10-12 00:00:00). Does anyone know a quick and easy way to just return the year?

Any help is appreciated. Thanks in advance!

+3


source to share


2 answers


you can use date api in angularjs

<span class="year"> {{ hit._highlightResult.original_release_date.value |  date:"yyyy" }} </span>

      

hit._highlightResult.original_release_date.value

should be (according to doc)

Date to be formatted as a date object, milliseconds (string or number), or various ISO 8601 date and time string formats (e.g. yyyy-MM-ddTHH: mm: ss.sssZ and shorter versions like yyyy-MM-ddTHH: mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). If the time zone is not specified in the string input, the time is considered to be in the local time zone.

create an object javascript date

and format it to display only the year,

step 1 - create filter to get date object from string (2012-10-12 00:00:00)



app.filter('dateToISO', function() {
    return  function(input) {
       var dateTime = input.split(" ");
       var date = dateTime[0];
       var datePartials = date.split("-");
       var time = dateTime[1];
       var timePartials = time.split(":");
       var formattedDate = new Date();
       formattedDate.setFullYear(datePartials[0]);
       formattedDate.setMonth(datePartials[1]-1);
       formattedDate.setDate(datePartials[2]);
       formattedDate.setHours(timePartials[0]);
       formattedDate.setMinutes(timePartials[1]);
       return formattedDate;
    };
});

      

step 2 - create a controller function to get date object

$scope.getDate = function() {
    return $filter('dateToISO')('2012-10-12 00:00:00');
};

      

step 3 - calling this function and getting the date object in HTML format

<span class="year"> {{ getDate() |  date:"yyyy" }} </span>

      

here is a working Demo Plunker

+3


source


It might be a little bit and cheating, but I think 2012-10-12 00: 00: 00'.split ('-') [0] will do the trick



0


source







All Articles