0

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.?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.