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:
Step 1: Recall Angular date pipe format codes
Angular uses lowercase 'yyyy' to represent the full year in date pipe formatting.
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.
Final Answer:
{{ myDate | date:'yyyy' }} -> Option A
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
Master "Pipes" in Angular
9 interactive learning modes - each teaches the same concept differently