How to get local date / time format?

I want to get the system date and time format and display date from the database in the same format. for example, if the system date format is similar to the dd / yyyy month , and the date stored in the database is the mm / dd / yyyy format , then I need to get the local date and time format and convert my saved date to local format. Entering HTML5 type="date" uses the default date / time format for this, as well as the display date in the same format. how can i do this using jquery or javascript .

+6
source share
1 answer

You can use a JavaScript Date object:

 var date = new Date(yearFromDb, monthFromDb, dayFromDb); date.toLocaleDateString(); 

EDIT

It seems that the above method is incompatible between different browsers (Chrome, IE, Firefox). Therefore, I think that your only option is to get a formatted date string from the server side.

Here is jsFiddle that shows a cross-browser way to get users locale if you can use it to get formatted date in current locale from backend (if any).

 var locale = window.navigator.userLanguage || window.navigator.language; 
0
source

Source: https://habr.com/ru/post/949437/


All Articles