views:

425

answers:

5

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"

A: 

Do you have to declare your date in this way? You can provide a number of different strings to the Date() function to be more specific.

Examples:

var d1 = new Date('January 1, 1970'); // Date as a parse
var d2 = new Date(0);                 // Date as a julian number
var d3 = new Date(1970, 0, 1);        // Date as arguments

http://www.hunlock.com/blogs/Javascript_Dates-The_Complete_Reference

So if you're relying on user input into text boxes, provide a TextBox for the month, one for the year, etc. Or use drop-down lists, since that's a very standard method for asking for date information. Then use the d3 example above.

*Edit - added after seeing the comment. *

Sorry that won't work. I really want to help out here, but I'm lacking information at this point..

OK.. So here's the next silly question I have.. If it's a read-only textbox, the data in the textbox has got to be coming from somewhere... Either a database, or some posted form, etc. Can this be handled server-side? Either format the date before it's output OR parse it with server-side code after it's submitted?

I'm not sure what tools you're using, but there must be a solution. You may be right, however, in there not being a way to do it in JavaScript. It would be very helpful if your you also had a way to determine the time zne and culture, so you would have a starting point with working on figuring what format the date string is in.

David Stratton
Have to use the way I described. Its a readonly textbox I'm reading the date string from
A: 

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?

Berry
Adding "86400*1000 milliseconds" fails twice a year in places that use daylight savings time.
Paul Tomblin
A: 

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.

kennebec
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.
TRiG
Good call- but it is such a long name. Maybe dmyDay()
kennebec
+2  A: 

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.

CMS
A: 

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.

peller