简体   繁体   中英

Angular js interpolation issue when string format

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"] 在此处输入图像描述

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

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM