Sravan2023
Sravan2023

Reputation: 176

JavaScript Date format conversion using locale information

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.

Upvotes: 4

Views: 4709

Answers (1)

gmaliar
gmaliar

Reputation: 5489

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"

Upvotes: 1

Related Questions