1
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".

ChrisF
  • 134,786
  • 31
  • 255
  • 325
Chintan Sukhadiya
  • 329
  • 1
  • 5
  • 10
  • var today = new Date(); return String((today.getDate()<=9)?'0'+today.getDate():today.getDate())+String((today.getMonth()+1)<=9?'0'+today.getMonth()+1:today.getMonth()+1)+String(today.getFullYear()) – Álvaro Touzón Jul 28 '17 at 15:12
  • 2
    Possible duplicate of [Getting current date and time in JavaScript](https://stackoverflow.com/questions/10211145/getting-current-date-and-time-in-javascript) – eshirima Jul 28 '17 at 15:20
  • @eshirima The OP appears to want to get the *format* not the date and time itself. – Heretic Monkey Jul 28 '17 at 18:32
  • It may be a duplicate of [how to get local date/time format?](https://stackoverflow.com/q/17647644/215552) or [JavaScript - Get system short date format](https://stackoverflow.com/q/16210058/215552). – Heretic Monkey Jul 28 '17 at 18:58

2 Answers2

2

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
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
0

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());

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

jmtalarn
  • 1,513
  • 1
  • 13
  • 16
  • 4
    That would get them the date, formatted in the correct locale, but not the format itself. Consider running this on November 11th. How could you tell if the format is MM/DD or DD/MM? – Heretic Monkey Jul 28 '17 at 18:32