transform(value: any): any {
var customdate = new DatePipe(navigator.language);
value = customdate.transform(value,"dd-mm-yyyy");
return value;
}
I need to get the date format of client's machine at the place of "dd-mm-yyyy".
transform(value: any): any {
var customdate = new DatePipe(navigator.language);
value = customdate.transform(value,"dd-mm-yyyy");
return value;
}
I need to get the date format of client's machine at the place of "dd-mm-yyyy".
Unfortunately, there does not seem to be a way to get the preferred format for dates from the browser. You can get certain information about the locale a user is in, and from that you might be able to divine the appropriate default values. As an example, the following snippet uses the ECMAScript Internationalization API (which was released after how to get local date/time format? was answered):
console.log(new Intl.DateTimeFormat().resolvedOptions())
/*
On my machine, this outputs:
{
"locale": "en-US",
"calendar": "gregory",
"numberingSystem": "latn",
"timeZone": "America/New_York",
"year": "numeric",
"month": "numeric",
"day": "numeric"
}
*/
From that information, you should be able to use a server-side language/framework to construct the necessary format. For instance, in .NET, using C#:
var culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
var dateTimeInfo = culture.DateTimeFormat;
var formats = dateTimeInfo.GetAllDateTimePatterns();
// formats now contains a list of formats for this culture
Just get the current date with the Javascript Date object.
var currentDate = new Date();
https://www.w3schools.com/js/js_dates.asp
and use then the toLocaleDateString() function to get in the locale format.
console.log(date.toLocaleDateString());