transform(value: any): any {
var customdate = new DatePipe(navigator.language);
value = customdate.transform(value,"dd-mm-yyyy");
return value;
}
我需要在“dd-mm-yyyy”处获取客户端机器的日期格式。
最佳答案
不幸的是,似乎没有办法从浏览器中获取日期的首选格式。您可以获得有关用户所在的语言环境 的某些信息,并且您可以从中推断出适当的默认值。例如,以下代码片段使用 ECMAScript 国际化 API(在回答 how to get local date/time format? 后发布):
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"
}
*/
根据这些信息,您应该能够使用服务器端语言/框架来构建必要的格式。例如,在 .NET 中,使用 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
关于javascript - 如何使用 angular 2 或 javascript 从客户端机器获取日期和时间格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45376909/