Home > Back-end >  Angular js interpolation issue when string format
Angular js interpolation issue when string format

Time:12-07

I am trying to format the string in the HTML. This is my html Sample

      <tr ng-repeat="dob in userId.dobs">
                <td align="center">{{$index   1}}</td>
                <td>{{dob.dateType}}</td>
                <td>{{dob.birthDate}}</td>
                <td>{{dob.dateYear}}</td>
                <td>{{dob.fromYear}}</td>
                <td>{{dob.toYear}}</td>
            </tr>

Currently my {{dob.fromYear}}show like 1973/01/21. But I need to split this string and keep only the year. Its like below {{dob.fromYear}} show like 1973. I tried to do it using below technique

 <td>{{dob.fromYear.split('/')}}</td>

now my result like ["1973","01","21"] enter image description here

I need to display only the year. How i do it.?

CodePudding user response:

You should create a pipe. Pass the dob string to the pipe and inside the pipe you can do some text manipulation to get only the year.

const year = "1111/22/33".split("/").slice(0,1).join()
return year;

This code first splits the string. Then slice(0,1) picks the year part and .join() converts the [1111] into a string.

CodePudding user response:

You can try

dob.fromYear.split('/')[0]

But i think this could be problematic if the date is in another format. So better to use:

new Date(dob.fromYear).getFullYear()
  • Related