Bird
0
0

Which of the following is the correct syntax to format a date using Angular's date pipe to show only the year in a template?

easy📝 Syntax Q12 of 15
Angular - Pipes
Which of the following is the correct syntax to format a date using Angular's date pipe to show only the year in a template?
A{{ myDate | date:'yyyy' }}
B{{ myDate | date:'YYYY' }}
C{{ myDate | date:'year' }}
D{{ myDate | date:'yy' }}
Step-by-Step Solution
Solution:
  1. Step 1: Recall Angular date pipe format codes

    Angular uses lowercase 'yyyy' to represent the full year in date pipe formatting.
  2. Step 2: Check each option's format string

    {{ myDate | date:'yyyy' }} uses 'yyyy' correctly; A uses uppercase which is invalid; C uses 'year' which is not a valid format; D uses 'yy' which shows two-digit year.
  3. Final Answer:

    {{ myDate | date:'yyyy' }} -> Option A
  4. Quick Check:

    Year format = 'yyyy' [OK]
Quick Trick: Use lowercase 'yyyy' for full year in date pipe [OK]
Common Mistakes:
  • Using uppercase 'YYYY' instead of 'yyyy'
  • Using descriptive words like 'year' instead of format codes
  • Using 'yy' which shows only two digits

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes