2

I want to know the date format that is using the current device, for example, is I call this function, this function will return me the current format

var a  = getDateFormat();
console.log(a);
MM/dd/YYYY

Anyone know how to archive that?

Hetdev
  • 1,425
  • 2
  • 21
  • 29
  • Electron is just JavaScript. AFAIK, JavaScript doesn't have a "device aware" DateTime format automatically resolved. Instead, it relies on the standard JS DateTime objects which are practically just a timestamp, that you can visualize using any custom DateTime format you like (dd/mm/yyyy, mm/dd/yyyy, etc) using something like this: https://stackoverflow.com/questions/25275696/javascript-format-date-time – Dzhuneyt May 15 '18 at 21:13
  • @hasMobi-AndroidApps yeah, but I can't rely on that, I need the parent format to apply it to other chilñd devices. Thanks for the contribution. – Hetdev May 15 '18 at 21:31

3 Answers3

1

There is currently no cross platform way in Electron to get the users preferred date format or decimal separator. You can guess users preference by looking at the response of app.getLocale() but you'll still find somebody who has overridden their systems local regional settings with their preferences.

Tim
  • 7,746
  • 3
  • 49
  • 83
1

Workaround to get system date format: https://github.com/electron/electron/issues/13023#issuecomment-418581463

(Tested on Windows 10)

Ahmad Radi
  • 144
  • 1
  • 5
0

Date format depends on localization setting. In electron you can use app.getLocale() to obtain locale string.

To get date format string (which should be well defined for locales), you can either create the mapping on your own or try to use some external libraries that already have that information. Moment.js can be useful, you can get dateFormat info from it (and also format date if that is your final goal).

Check this fiddle to see how to get longDateFormat using locale string.

let l = moment().locale('en');
console.log(l.localeData().longDateFormat('L'));
l = moment().locale('es');
console.log(l.localeData().longDateFormat('L'));
Jakub Piskorz
  • 1,023
  • 12
  • 21