4

I need to generate a UK date format using a US date format and locale as "en_GB" in JavaScript. The problem is that my UI returns a date selected by the user in the format of the current user's locale and I want to compare it with another date that is coming from an application that always gives me only one format [MM/DD/YY]

Now in the JavaScript on the page I was able to pass these two dates but since the format is different, the comparison fails.

if(Date.parse(selReqDate) < Date.parse(curDate))

The inputs I have are:

locale : en_GB
uk locale selecteddate : 01/08/2005 [DD/MM/YY]
us locale currentdate  : 08/01/2005 [MM/DD/YY]

I have the same issue with all locales other than US.

gariepy
  • 3,576
  • 6
  • 21
  • 34
Sravan2023
  • 176
  • 1
  • 2
  • 12

1 Answers1

1

use toLocaleDateString

var date = new Date(Date.UTC(2012, 11, 11, 3, 0, 0));
date.toLocaleDateString('en-GB'); // "11/12/2012"
date.toLocaleDateString('en-US'); // "12/11/2012"
gmaliar
  • 5,294
  • 1
  • 28
  • 36
  • can u automatically format this according to the OS locale? – vsync Jun 18 '14 at 21:46
  • You should use something of the sort of this, http://stackoverflow.com/questions/673905/best-way-to-determine-users-locale-within-browser – gmaliar Jun 19 '14 at 13:40