2

It appears I can't use the javascript Date object as it inherintly defaults to US dates when you initialise with a datestring. There is no way of passing any culture information to the date object

I.e. No matter what the clients locale settings are

var d = new Date("08/10/2009") will always create a date object representing the 10th August 2009 rather than the 8th October 2009 if the clients locale was the UK.

So given that my requirement is to be able to add/subtract days/months/years easily is there a clever way of doing this easily without the Date object

All i need to do is add a day to a date (or a string representation of a date). so if my code detects the locale setttings are in the US, when it sees a string like "10/08/2009" it whacks it up to "10/09/2009" but if it had detected it was in the UK it would have know it a uk string representation of a date and whacked it up to "09/10/2009"

4 Answers 4

3

For date manipulation and localization on JavaScript I always recommend the DateJS library.

This library abstracts the use of Date objects, has a very good localization options, powerful date parsing and formatting, and it also has a very nice fluent API.

Sign up to request clarification or add additional context in comments.

Comments

2

If you know you are getting input formatted dd/mm/yyyy you can easily assemble the correct date.

function britDay(D){
 D= D.match(/\d+/g);
 return new Date(+D[2], D[1]-1, +D[0]);
}

toLocaleDateString will return the date in the format expected by the user.

Relying on the user input that obeys particular formatting rules is optimistic- which is why most sites use separate, labeled inputs or select fields for the month, date and year.

3 Comments

Thats the thing I don't know what way it will be formatted cos it will depend on what the locale settings are
I'd call the function internationalDay() not britDay(). It's only the US that uses weird date style.
Good call- but it is such a long name. Maybe dmyDay()
0

You probably know that it's easy to add one day to a date, just add 86,400 * 1000 milliseconds to the date. It sounds like displaying in your locale is the issue; does Date.toLocaleString() not do the right thing for you?

1 Comment

Adding "86400*1000 milliseconds" fails twice a year in places that use daylight savings time.
0

dojo.date.locale.parse will be able to parse a formatted string according the locale of your choice. It has a table of cultural data based off unicode.org/cldr. See this article for more information.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.