Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2 date pipe locale_id period

I have a problem with the angular2 date pipe when using danish locale.

When I format a date like:

{{myDate | date:'dd-MM-yyyy'}} 

it outputs the day of the date with a suffixed period like this:

17.-03-2017

allthough I would expect it to be like this:

17-03-2017

The locale is set in the app.module like this:

providers: [ {provide: LOCALE_ID, useValue: 'da-DK'} ]  

I have made this plnkr to make it more clear http://plnkr.co/edit/A5ddrKP5cmsSZ9bTqzPh

UPDATE

It probably has something to do with the way dates are formatted in danish. Se below:

var locale = 'da-DK'; 
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; 
var date = new Date(2017,2,17); var result = new Intl.DateTimeFormat(locale, options).format(date); 
alert(result); 

turns into --> fredag den 17. marts 2017

Notice the dot

like image 951
renemundt Avatar asked Mar 17 '17 15:03

renemundt


People also ask

How do I set the date pipe locale?

As of Angular2 RC6, you can set default locale in your app module, by adding a provider: @NgModule({ providers: [ { provide: LOCALE_ID, useValue: "en-US" }, //replace "en-US" with your locale //otherProviders... ] }) The Currency/Date/Number pipes should pick up the locale.

What angular pipe can we use to show a date with a specific format?

Angular date pipe used to format dates in angular according to the given date formats,timezone and country locale information. Using date pipe, we can convert a date object, a number (milliseconds from UTC) or an ISO date strings according to given predefined angular date formats or custom angular date formats.

What is date pipeline?

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.


1 Answers

localization is "platform independent", ECMAscript provides specifications and each Javascript engine provides its own implementation.

There is also a compatibility implementation, Intl.js, which will provide the API if it doesn't already exist. You can see the implementation for the locale Danish (Denmark) here, you will see the available formats, and the day always returns the period after.

 "availableFormats": {
                "d": "d.",
                "E": "ccc",
                "Ed": "E 'den' d.",
                "Ehm": "E h.mm a",
                "EHm": "E HH.mm",
                "Ehms": "E h.mm.ss a",
                "EHms": "E HH.mm.ss",
                "Gy": "y G",
                "GyMMM": "MMM y G",
                "GyMMMd": "d. MMM y G",

You could create a custom pipe, and chain it to remove the period

{{myDate | date:'dd-MM-yyyy'|removePeriod}}

@Pipe({name: 'removePeriod'})
    export class RemovePeriodPipe implements PipeTransform {
      transform(input: string) {
        return input.replace(/\./g, '');
      }
    }
like image 192
Eduardo Dennis Avatar answered Oct 23 '22 05:10

Eduardo Dennis