ANGULAR
Complete Angular Tutorial For Beginners Introduction to Angular | What is Angular? Architecture Overview & Concepts of Angular How to Install Angular How to Create a new project in Angular Bootstrapping in Angular: How It Works Internally Angular Components Overview & Examples Data Binding in Angular Interpolation in Angular Property Binding in Angular Event Binding in Angular ngModel & Two way Data binding in Angular NgModelChange & Change Event in Angular Child/Nested Components in Angular angular directives angular ngFor directive ngSwitch, ngSwitchcase, ngSwitchDefa ult Angular Example How to use ngIf, else, then in Angular By example NgClass Example Conditionally apply class Angular ngStyle Directive Angular Trackby to improve ngFor Performance How to Create & Use Custom Directive In Angular Working with Angular Pipes How to Create Custom Pipe in Angular Formatting Dates with Angular Date Pipe Using Angular Async Pipe with ngIf & ngFor angular keyValue pipe Using Angular Pipes in Components or Services Angular Component Communication & Sharing Data Angular Pass data to child component Angular Pass data from Child to parent component Component Life Cycle Hooks in Angular Angular ngOnInit And ngOnDestroy Life Cycle hook Angular ngOnChanges life Cycle Hook Angular ngDoCheck Life Cycle Hook Angular Forms Tutorial: Fundamentals & Concep t s Angular Template-driven forms example How to set value in template-driven forms in Angular Angular Reactive Forms Example Using Angular FormBuilder to build Forms SetValue & PatchValue in Angular StatusChanges in Angular Forms ValueChanges in Angular Forms FormControl in Angular FormGroup in Angular Angular FormArray Example Nested FormArray Example Add Form Fields Dynamically SetValue & PatchValue in FormArray Angular Select Options Example in Angular Introduction to Angular Services Introduction to Angular Dependency Injection Angular Injector, @Injectable & @Inject Angular Providers: useClass, useValue, useFactory & useExisting Injection Token in Angular How Dependency Injection & Resolution Works in Angular Angular Singleton Service ProvidedIn root, any & platform in Angular @Self, @SkipSelf & @Optional Decorators Angular '@Host Decorator in Angular ViewProviders in Angular Angular Reactive Forms Validation Custom Validator in Angular Reactive Form Custom Validator with Parameters in Angular Inject Service Into Validator in Angular template_driven_form_validation_in_angular Custom Validator in Template Driven Forms in Angular Angular Async Validator Example Cross Field or Multi Field Validation Angular How to add Validators Dynamically using SetValidators in Angular Angular HttpClient Tutorial & Example Angular HTTP GET Example using httpclient Angular HTTP POST Example URL Parameters, Query Parameters, httpparams in Angular HttpClient Angular HTTPHeaders Example Understanding HTTP Interceptors in Angular Angular Routing Tutorial with Example Location Strategy in Angular Angular Route Params Angular : Child Routes / Nested Route Query Parameters in Angular Angular Pass Data to Route: Dynamic/Static RouterLink, Navigate & NavigateByUrl to Navigate Routes RouterLinkActive in Angular Angular Router Events ActivatedRoute in Angular Angular Guards Tutorial Angular CanActivate Guard Example Angular CanActivateChild Example Angular CanDeactivate Guard Angular Resolve Guard Introduction to Angular Modules or ngModule Angular Routing between modules Angular Folder Structure Best Practices Guide to Lazy loading in Angular Angular Preloading Strategy Angular CanLoad Guard Example Ng-Content & Content Projection in Angular Angular @input, @output & EventEmitter Template Reference Variable in Angular ng-container in Angular How to use ng-template & TemplateRef in Angular How to Use ngTemplateOutlet in Angular '@Hostbinding and @Hostlistener_in_Angular Understanding ViewChild, ViewChildren &erylist in Angular ElementRef in Angular Renderer2 Example: Manipulating DOM in Angular ContentChild and ContentChildren in Angular AfterViewInit, AfterViewChecked, AfterContentInit & AfterContentChecked in Angular Angular Decorators Observable in Angular using RxJs Create observable from a string, array & object in angular Create Observable from Event using FromEvent in Angular Using Angular observable pipe with example Angular Map Operator: Usage and Examples Filter Operator in Angular Observable Tap operator in Angular observable Using SwitchMap in Angular Using MergeMap in Angular Using concatMap in Angular Using ExhaustMap in Angular Take, TakeUntil, TakeWhile & TakeLast in Angular Observable First, Last & Single Operator in Angular Observable Skip, SkipUntil, SkipWhile & SkipLast Operators in Angular The Scan & Reduce operators in Angular DebounceTime & Debounce in Angular Delay & DelayWhen in Angular Using ThrowError in Angular Observable Using Catcherror Operator in Angular Observable ReTryWhen inReTry, ReTryWhen in Angular Observable Unsubscribing from an Observable in Angular Subjects in Angular ReplaySubject, BehaviorSubject & AsyncSubject in Angular Angular Observable Subject Example Sharing Data Between Components Angular Global CSS styles View Encapsulation in Angular Style binding in Angular Class Binding in Angular Angular Component Styles How to Install & Use Angular FontAwesome How to Add Bootstrap to Angular Angular Location Service: go/back/forward Angular How to use APP_INITIALIZER Angular Runtime Configuration Angular Environment Variables Error Handling in Angular Applications Angular HTTP Error Handling Angular CLI tutorial ng new in Angular CLI How to update Angular to latest version Migrate to Standalone Components in Angular Create Multiple Angular Apps in One Project Set Page Title Using Title Service Angular Example Dynamic Page Title based on Route in Angular Meta service in Angular. Add/Update Meta Tags Example Dynamic Meta Tags in Angular Angular Canonical URL Lazy Load Images in Angular Server Side Rendering Using Angular Universal The requested URL was not found on this server error in Angular Angular Examples & Sample Projects Best Resources to Learn Angular Best Angular Books in 2020

Formatting Dates with Angular Date Pipe

Angular Date Pipe allows us to format dates in Angular using the requested format, time zone & local information. It comes with built-in pre-defined formats. We can also customize the date format by creating custom format strings. We can set the time zone, country locale, etc. This tutorial shows how to use Date Pipe using examples.

Using Date Pipe

The Date uses the pipe operator i.e |. Specify the date_expression, which you want to format in the left side of the |. On the right side specify date followed by the arguments. It accepts three arguments format, timezone & locale

                              

 
{{ date_expression | date [ : format [ : timezone [ : locale ] ] ] }}
                            
                        

Date Pipe Example

The following is the example of a date pipe it its simplest form.

                              

<h3>Using Date Pipe </h3>
<p>Unformatted date : {{toDate }} </p>     //Without pipe
<p>Formatted date : {{toDate | date}} </p>   //With Date Pipe
                            
                        
image

Date Expression

The Date Expression can be anything that evaluates to date. For example it can be a Date object, a number (milliseconds since UTC epoch), or an ISO string

                              

//Component
toDate: Date = new Date();
numDate=1590319189931;
strDate="Sun May 24 2020 19:16:23";
                            
                        
                              
 
<h3>Date Expression </h3>
<p>Date Object : {{toDate | date}} </p>       //May 24, 2020
<p>Number Date : {{numDate | date}} </p>      //May 24, 2020
<p>ISO Date : {{strDate | date}} </p>         //May 24, 2020
 
                            
                        

Parameters to Date Pipe

The Date pipe accepts three arguments format, timezone & locale

Parameter Data Type Particulars
format string There are two types of format.
  1. predefined formats
  2. Custom format string

Default: mediumDate.
timezone string Use
  • A timezone offset (such as '+0430')
  • Standard UTC/GMT
  • continental timezone abbreviation.

  • Default: End-users local system timezone
    locale string A locale code for the locale format rules to use.
    Default: The value of LOCALE_ID ( which is en-US)

    How to Change LOCALE_ID

    Date Format

    There are two types formats options available

    1. Pre defined Format
    2. Custom Format string

    Pre defined formats

    The following are the Pre defined formats that you can use. We have also mentioned the corresponding custom formats next to it.

    Format Equivalent Custom Format Example Result
    short 'M/d/yy, h:mm a' {{toDate | date:'short'}} 5/24/20, 3:40 PM
    medium 'MMM d, y, h:mm:ss a' {{toDate | date:'medium'}} May 24, 2020, 3:42:17 PM
    long 'MMMM d, y, h:mm:ss a z' {{toDate | date:'long'}} May 24, 2020 at 3:42:17 PM GMT+5
    full 'EEEE, MMMM d, y, h:mm:ss a zzzz' {{toDate | date:'full'}} Sunday, May 24, 2020 at 3:42:17 PM GMT+05:30
    shortDate 'M/d/yy' {{toDate | date:'shortDate'}} 5/24/20
    mediumDate 'MMM d, y' {{toDate | date:'mediumDate'}} May 24, 2020
    longDate 'MMMM d, y' {{toDate | date:'longDate'}} May 24, 2020
    fullDate 'EEEE, MMMM d, y' {{toDate | date:'fullDate'}} Sunday, May 24, 2020
    shortTime 'h:mm a' {{toDate | date:'shortTime'}} 3:42 PM
    mediumTime 'h:mm:ss a' {{toDate | date:'mediumTime'}} 3:42:17 PM
    longTime 'h:mm:ss a z' {{toDate | date:'longTime'}} 3:42:17 PM GMT+5
    fullTime 'h:mm:ss a zzzz' {{toDate | date:'fullTime'}} 3:42:17 PM GMT+05:30

    Custom Format string

    The following is the complete list of custom formats that are available

    Field type Format Description Example Value
    Era G, GG & Abbreviated AD
    GGG
    GGGG Wide Anno Domini
    GGGGG Narrow A
    Year y Numeric: minimum digits 2, 20, 201, 2017, 20173
    yy umeric: 2 digits + zero padded 02, 20, 01, 17, 73
    yyy Numeric: 3 digits + zero padded 002, 020, 201, 2017, 20173
    yyyy Numeric: 4 digits or more + zero padded 0002, 0020, 0201, 2017, 20173
    Month M Numeric: 1 digit 9, 12
    MM Numeric: 2 digits + zero padded 09, 12
    MMM Abbreviated Sep
    MMMM Wide September
    MMMMM Narrow S
    Month L Numeric: 1 digit 9, 12
    standalone LL Numeric: 2 digits + zero padded 09, 12
    LLL Abbreviated Sep
    LLLL Wide September
    LLLLL Narrow S
    Week of year w Numeric: minimum digits 1... 53
    ww Numeric: 2 digits + zero padded 01... 53
    Week of month W Numeric: 1 digit 1... 5
    Day of month d Numeric: minimum digits 1
    dd Numeric: 2 digits + zero padded 01
    Week day E, EE & EEE Abbreviated Tue
    EEEE Wide Tuesday
    EEEEE Narrow T
    EEEEEE Short Tu
    Period a, aa & aaa Abbreviated am/pm or AM/PM
    aaaa Wide (fallback to a when missing) ante meridiem/post meridiem
    aaaaa Narrow a/p
    Period* B, BB & BBB Abbreviated mid
    BBBB Wide am, pm, midnight, noon, morning, afternoon, evening, night
    BBBBB Narrow md.
    Period standalone* b, bb & bbb Abbreviated mid.
    bbbb Wide am, pm, midnight, noon, morning, afternoon, evening, night
    bbbbb Narrow md
    Hour 1-12 h Numeric: minimum digits 1, 12
    hh Numeric: 2 digits + zero padded 01, 12
    Hour 0-23 H Numeric: minimum digits 0, 23
    HH Numeric: 2 digits + zero padded 00, 23
    Minute m Numeric: minimum digits 8, 59
    mm Numeric: 2 digits + zero padded 08, 59
    Second s Numeric: minimum digits 0... 59
    ss Numeric: 2 digits + zero padded 00... 59
    Fractional seconds S Numeric: 1 digit 0... 9
    SS Numeric: 2 digits + zero padded 00... 99
    SSS Numeric: 3 digits + zero padded (= milliseconds) 000... 999
    Zone z, zz & zzz Short specific non location format (fallback to O) GMT-8
    zzzz Long specific non location format (fallback to OOOO) GMT-08:00
    Z, ZZ & ZZZ ISO8601 basic format -0800
    ZZZZ Long localized GMT format GMT-8:00
    ZZZZZ ISO8601 extended format + Z indicator for offset 0 (= XXXXX) -08:00
    O, OO & OOO Short localized GMT format GMT-8
    OOOO Long localized GMT format GMT-08:00

    Custom Format example

                                  
    
    {{toDate | date:'dd/MM/y'}}                //24/05/2020
     
    {{toDate | date:'dd/MM/yy HH:mm'}}         //May 24, 2020, 7:17:26 PM
                                
                            

    Timezone Example

    The following examples, shows how to use time zones

                                  
    
    Date in India (IST Time Zone)  : {{toDate | date:'short':'IST'}}     //Date in India (IST Time Zone) : 5/24/20, 7:32 PM
     
    Date  in USA (CDT Time Zone)   : {{toDate | date:'short':'CDT'}}    //Date in USA (CDT Time Zone) : 5/24/20, 9:02 AM
     
    Date in India (+0530)     : {{toDate | date:'short':'+0530'}}     //Date in India (+0530) : 5/24/20, 7:32 PM
     
    Date in USA (-0700)     : {{toDate | date:'short':'-0500'}}    //Date in USA (-0700) : 5/24/20, 9:02 AM
     
                                
                            

    Country Locale Example

    The Country Locale is the third argument.

                                  
     
    British date time is {{toDate | date:'dd/MM/yy HH:mm':'GMT':'en-GB'}}       //British date time is 24/05/20 14:26