2

Current application is implemented by JSP, so it will pass localized Date (in MM/DD/YYYY or DD/MM/YYYY depends on the locale) on the input field entered by user. Then javascript is supposed to identify that date and filter out data (i.e. orders that have been placed) on the UI with the same date.

Just like user enter a date(MM/DD/YYYY or DD/MM/YYYY depends on the locale) then the list of orders on the UI should only remain the ones created on the same date So basically, javascript need to fetch the date entered by user and dates from all orders, to make a compare.

code sample is like this

if(aOrder_date != null ){
    if(aOrder_date != '' && aOrder_date != 'undefined'
        && order_date.getTime() != aOrder_date.getTime()){
        showUp = false;
    }
}

So now the issue is, even JSP(Java) is on Spanish locale, but javascript still take it with English locale. then:

08/03/2013 

on the input field is

March, 08, 2013 in Spanish Calendar

but it is interpreted as

Auguest, 03, 2013 in English Calendar

by javascript

Also javascript cannot identify any date after the 12th(make sense as javascript twist month and day) when the page is switched to Spanish.

So is there anyway to put or sync locale in Javascript then make above working properly?

Dreamer
  • 7,333
  • 24
  • 99
  • 179
  • A `Date` object isn't localized - it's just a number of milliseconds since the Unix epoch. It's really unclear exactly where things are happening here, but it sounds like you should be simply converting the `Date` to a `String` using a fixed format. – Jon Skeet Sep 26 '13 at 15:38
  • @Jon Skeet Thanks but then how can I compare the two dates if they are on string type? – Dreamer Sep 26 '13 at 15:39
  • What do you mean by "on string type"? Your whole question is very confusing at the moment. Please edit it to clarify. – Jon Skeet Sep 26 '13 at 15:41
  • I suggest checking out the [moment.js library](http://momentjs.com/) which is built to help with stuff like this specifically [this bit on formatting](http://momentjs.com/docs/#/parsing/string-format/) – Plato Sep 26 '13 at 15:42
  • Since you haven't posted any code, I can only say: based on the locale chosen by user, have a `dateToUse` object that will be the string representation of your date, and apply a date formatting to your date always to `MM/dd/yyyy`. – Luiggi Mendoza Sep 26 '13 at 15:43
  • Your input date is ambiguous. You have to decide which input formats you accept based on the users locale. Maybe you need to allow the user to specify the locale they want to use, or use the browser's default based on the language/location. Then you use the same format when outputting the same date – Ruan Mendes Sep 28 '13 at 13:22

2 Answers2

1

Hope this link may help you.

Display date/time in user's locale format and time offset

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

or you can use this parsing methods for date.

  toISOString() 

or

  getUTCDate(), getUTCMonth() and getUTCDay()

or

  dateString.toLocaleString()
Community
  • 1
  • 1
Deepak Kumar Jha
  • 462
  • 6
  • 15
0

You can use datejs. In the downloads you can find localized versions of datejs.

Note that the active locale can be dynamically established by code instead of detected by the browser's context via loading the different versions available (date-es-AR.js, date-en-GB.js, ...).

So finally you can use Date.parse with your established locale.

Community
  • 1
  • 1
nilsandrey
  • 1,030
  • 11
  • 28