如何获取日期和日期从客户端计算机使用angular 2或javascript的时间格式? [英] How to get the Date & Time format From Client Machine using angular 2 or javascript?

查看:65
本文介绍了如何获取日期和日期从客户端计算机使用angular 2或javascript的时间格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

transform(value: any): any {
    var customdate = new DatePipe(navigator.language);
    value = customdate.transform(value,"dd-mm-yyyy");
    return value;
}   

我需要在"dd-mm-yyyy"位置获取客户端计算机的日期格式.

I need to get the date format of client's machine at the place of "dd-mm-yyyy".

推荐答案

不幸的是,似乎没有一种方法可以从浏览器中获取日期的首选格式.您可以获取有关用户所在的语言环境的某些信息,并由此可以确定适当的默认值.例如,以下代码段使用ECMAScript国际化API(在如何获取本地日期/时间格式之后发布的版本?已回答):

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"
}
*/

根据这些信息,您应该能够使用服务器端语言/框架来构造必要的格式.例如,在.NET中,使用C#:

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

这篇关于如何获取日期和日期从客户端计算机使用angular 2或javascript的时间格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆