Please Help with Dates

B

Brent Webster

This problem seems to occur dependant on the computer system settings.
I have no issues when the system date is set as M/d/yyyy but I do
have a problem with the system date set as d/M/yyyy.

I have a text box on a web page that holds the date ie)
document.frmTimesheet.tWeekFrom.value = 19/12/2004

theDate = new Date(document.frmTimesheet.tWeekFrom.value);

// split into day, month, year
iDay = theDate.getDate();
iMonth = theDate.getMonth()+1;
iYear = theDate.getFullYear();

alert(iDay);
alert(iMonth);
alert(iYear);

iday= 12
iMonth= 07
iYear= 2005

If there is any way to do this any help would be greatly appreciated.
 
M

Michael Winter

[snip]
document.frmTimesheet.tWeekFrom.value = 19/12/2004

theDate = new Date(document.frmTimesheet.tWeekFrom.value);

There is no concrete definition that specifies how a string should be
parsed. It's entirely up to the implementation. That may mean that parsing
would depend on the locale settings, or it may be fixed to a certain set
of formats.

In my opinion, it's far safer to parse the string yourself and use the
other form of Date constructor:

var date = new Date(year, month, day, hr, min, sec, ms);

though in your case, you only need to worry about the first three
arguments.

Of course, the other concern is what formats you accept. It's best to
avoid dd/mm/yyyy and mm/dd/yyyy unless you'll only attract audiences that
uses one of those.

[snip]

Mike
 
E

Evertjan.

Brent Webster wrote on 14 dec 2004 in comp.lang.javascript:
document.frmTimesheet.tWeekFrom.value = 19/12/2004

theDate = new Date(document.frmTimesheet.tWeekFrom.value);

d = "19/12/2004"

a = d.split("/")

theDate = new Date(a[2],a[1]-1,a[0]);
 
R

RobG

Brent said:
Is there a way to determine how the user has their computer settings
configured?

There is no need to.

[...]
A calendar control is loading the text boxes and uses the system date
including the format.

Then this is the source of your problem - don't do it. Set the start
date from the server, not the system. Pass it as a meta tag, HTML
hidden field, javascript variable, whatever but do not rely on the
client having a particular format, your ability to detect it or that it
is even correct.

What if it's set to 12-jan-2005 format? Or ISO8601 2005-01-12 format?
So you must use some other method.
If I do not know how the system is looking at this, how can I parse it
correctly.

You don't need to parse it, Mike's advice is sound. If it is a date
object, the month will *always* be returned by getMonth() within the
reliability constraints of javascript, whether or not it is enabled and
the vagaries of various browsers.
If system date format = d/m/yy then
d = "13/12/2004" 'This would be the passed date
a = d.split("/")
theDate = new Date(a[2],a[1]-1,a[0]);

Why mess with system date at all? Presumably you only need to deal
with:

1. user input dates: use a date picker or selection list or some
enforced method where users select the date rather than key it in.

2. JavaScript generated dates: you are in complete control and the
system settings are (more or less) irrelevant.
Else
d = "12/13/2004" 'This would be the passed date
theDate = new Date(d);

As Mike said:

d = "12/13/2004";
dA = d.split('/');
theDate = new Date(dA[2],dA[1]-1,d[0]);

Ensure that the date string is input according to *your* specification
by using a date picker (my preference, but tastes differ), option list,
thorough validation, whatever.

But you can't rely on detecting the system date format, nor that it is
accurate or correct.

And, to pre-empt the good Dr. J, you will likely need to allow for
different timezones and daylight saving.
 
F

Fred Oz

Brent Webster wrote:
[...]
A calendar control is loading the text boxes and uses the system date
including the format.
If I do not know how the system is looking at this, how can I parse it
correctly.

This is your error. If you want the current date of the client, then

var currentDate = new Date();

will create a date object that has it. I fail to see how system
settings have anything to do with it. Note that you have no idea if
the client has the date or time set correctly. Now you can get the
date, month and year directly using getDate(), getMonth() and
getFullYear() and all the following becomes irrelevant.

I would base everything on a date send by the server in the page, and
allow for the fact that it may be a day out either way. Use it as the
basis of a date selector that you control the format of, then the
system format is of no concern to you.

Say you send the date 13-feb-2005. Your date selector may start from
the Monday prior or maybe the Monday prior less one week, whatever.

[...]
 
E

Evertjan.

Brent Webster wrote on 15 dec 2004 in comp.lang.javascript:
If system date format = d/m/yy then
d = "13/12/2004" 'This would be the passed date
a = d.split("/")
theDate = new Date(a[2],a[1]-1,a[0]);

Else
d = "12/13/2004" 'This would be the passed date
theDate = new Date(d);

end if

Never do that, mixing your own parsing with that of the user.
 
M

Michael Winter

Is there a way to determine how the user has their computer settings
configured?

No, but even if you could, you're assuming that users have set their
locale options correctly. Some don't.
I was thinking of using a verify but if the month is valid it will still
be wrong

eg.) 06/10/2004 will be a valid date but if d/m/y is set it will parse
differently vs. m/d/y in Windows

You will have to limit the formats that you'll accept. Either use an
unambigous format like yyyy-mm-dd, or choose only *one* of dd/mm/yyyy or
mm/dd/yyyy. Depending on your audience, you might also be able to use
dd-MM-yyyy (for example, 15-Dec-2004).

[snip]

Mike
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Tue, 14 Dec 2004 11:44:50, seen in Brent
Webster said:
This problem seems to occur dependant on the computer system settings.
I have no issues when the system date is set as M/d/yyyy but I do
have a problem with the system date set as d/M/yyyy.

I have a text box on a web page that holds the date ie)
document.frmTimesheet.tWeekFrom.value = 19/12/2004

theDate = new Date(document.frmTimesheet.tWeekFrom.value);

// split into day, month, year
iDay = theDate.getDate();
iMonth = theDate.getMonth()+1;
iYear = theDate.getFullYear();

alert(iDay);
alert(iMonth);
alert(iYear);

iday= 12
iMonth= 07
iYear= 2005

// or alert((iYear*100+iMonth)*100+iDay)
// or alert(iYear + '-' + iMonth + '-' + iDay)
If there is any way to do this any help would be greatly appreciated.


You have called for, and been given, the twelfth day of the nineteenth
month of 2004.

You are in Canada; your software (undefined) is probably of US origin.
You should know that they do horrible things with dates & times, and are
very poor at international compatibility; but that the rest of the world
has perforce learnt to deal with that.

**At least** some browsers ignore locale when reading date strings.

Using "2004/12/19" may be safe; at least it only has one plausible
Gregorian interpretation.

ISTM that you did not read the Newsgroup FAQ before asking; it contains
the word "dates". See below, and js-date9.htm#CoO .
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Wed, 15
Dec 2004 07:13:11, seen in RobG
Why mess with system date at all? Presumably you only need to deal
with:

1. user input dates: use a date picker or selection list or some
enforced method where users select the date rather than key it in.


Only suitable for occasional use, since typing a date in a format to
which one has become accustomed is quicker than using selection
techniques (unless the selector can predict likely dates, and show
them[+]). ISTM that a data entry clerk would not like many forms of
picker.

[+] My js-date9.htm#DP should, for hotel bookings, be quicker than my
js-date9/htm#DDD ; but the latter should be quicker for entering DoB of
random OAPs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top