6

I want to get systems local date/time format and display date from database on this same format. e.g if system date format is like Month dd/yyyy and date stored in database is mm/dd/yyyy format then I need to get local date/time format and convert my stored date into local format. HTML5 input type="date" this takes default systems date/time format and show date in same format. how can I do this with jquery or javascript.

000
  • 26,951
  • 10
  • 71
  • 101
Prasad
  • 1,783
  • 5
  • 22
  • 26
  • 2
    So what is your question? and what have you tried so far? – Dhaval Marthak Jul 15 '13 at 06:01
  • 1
    I want to read local date/time format and display stored date in that format. I am not able to get system date format like mm/dd/yyyy or dd/mm/yyyy etc. – Prasad Jul 15 '13 at 06:03
  • http://stackoverflow.com/questions/8469436/how-format-javascript-date-with-regard-to-the-browser-culture – Satpal Jul 15 '13 at 06:04
  • possible duplicate of [JavaScript - Get system short date format](http://stackoverflow.com/questions/16210058/javascript-get-system-short-date-format) – Pekka Jul 15 '13 at 06:05
  • 1
    I hope you're not storing a date as a `mm/dd/yyyy` string in your database. You should be storing it as a `date` (or `datetime`) - the ANSI SQL format for these is `YYYY-MM-DD` (of course this does not address your question about locale settings) – Stephen P Jul 15 '13 at 06:26

1 Answers1

1

You could use the JavaScript Date object:

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

EDIT

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

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

var locale = window.navigator.userLanguage || window.navigator.language;
huysentruitw
  • 27,376
  • 9
  • 90
  • 133
  • Its working but its showing different format in chrome and mozilla, like in mozilla its showing Monday July 7 2013 and in chrome it is showing 7/15/2013. My local date/time format is mm/dd/yyyy. so do I have any other function which can return same format on all browser. – Prasad Jul 15 '13 at 06:16
  • I don't have Firefox installed, but it is possible that there are user preferences in the browsers configuration that override the default locale. Anyway, toLocaleDateString() should return a formatted date according to the user preferences. – huysentruitw Jul 15 '13 at 06:20
  • I checked with chromes setting but it does not taking any change, it still showing date in mm/dd/yyyy. even if I change my system date format(dd/mmm/yy) it is still showing date in mm/dd/yyyy – Prasad Jul 15 '13 at 07:23
  • Yes I did some additional tests and chrome always outputs the same. If you read the documentation of `toLocaleDateString()`, you'll find out that you can pass additional options. But IE and Chrome seem to ignore them. Are you using any kind of server-side script (PHP/ASP.NET, ...)? – huysentruitw Jul 15 '13 at 07:27
  • I am using ASP.NET MVC4 and one more thing i am changing date format through control panel(in Region and Language) will toLocaleDateString() take those change? – Prasad Jul 15 '13 at 08:04