Please Help with Dates | | |
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. | | | | re: Please Help with Dates
On 14 Dec 2004 11:44:50 -0800, Brent Webster <brent@webstyler.ca> wrote:
[snip]
[color=blue]
> document.frmTimesheet.tWeekFrom.value = 19/12/2004
>
> theDate = new Date(document.frmTimesheet.tWeekFrom.value);[/color]
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
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail. | | | | re: Please Help with Dates
Brent Webster wrote on 14 dec 2004 in comp.lang.javascript:
[color=blue]
> document.frmTimesheet.tWeekFrom.value = 19/12/2004
>
> theDate = new Date(document.frmTimesheet.tWeekFrom.value);
>
>[/color]
d = "19/12/2004"
a = d.split("/")
theDate = new Date(a[2],a[1]-1,a[0]);
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress) | | | | re: Please Help with Dates
Is there a way to determine how the user has their computer settings
configured?
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
Thinks it is Oct 10/2004 instead of June 10/2004
and 13/06/2004 is 01/06/2005 moving the month from 13 to 1 and advancing
the year by one
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.
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
Thanks,
Brent
replace nospam with .ca for correct e-mail address
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it! | | | | re: Please Help with Dates
Is there a way to determine how the user has their computer settings
configured?
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
Thinks it is Oct 10/2004 instead of June 10/2004
and 13/06/2004 is 01/06/2005 moving the month from 13 to 1 and advancing
the year by one
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.
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
Thanks,
Brent
replace nospam with .ca for correct e-mail address
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it! | | | | re: Please Help with Dates
Brent Webster wrote:[color=blue]
> Is there a way to determine how the user has their computer settings
> configured?[/color]
There is no need to.
[...][color=blue]
> A calendar control is loading the text boxes and uses the system date
> including the format.[/color]
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.
[color=blue]
> If I do not know how the system is looking at this, how can I parse it
> correctly.[/color]
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.
[color=blue]
> 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]);[/color]
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.
[color=blue]
> Else
> d = "12/13/2004" 'This would be the passed date
> theDate = new Date(d);[/color]
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.
--
Cheers, Rob | | | | re: Please Help with Dates
Brent Webster wrote:
[...][color=blue]
> 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.[/color]
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.
[...]
--
Fred. | | | | re: Please Help with Dates
Brent Webster wrote on 15 dec 2004 in comp.lang.javascript:
[color=blue]
> 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
>[/color]
Never do that, mixing your own parsing with that of the user.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress) | | | | re: Please Help with Dates
On 14 Dec 2004 23:25:22 -0600, Brent Webster <brent@webstyler.nospam>
wrote:
[color=blue]
> Is there a way to determine how the user has their computer settings
> configured?[/color]
No, but even if you could, you're assuming that users have set their
locale options correctly. Some don't.
[color=blue]
> 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[/color]
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
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail. | | | | re: Please Help with Dates
JRS: In article <125b53c.0412141144.52b02428@posting.google.com> , dated
Tue, 14 Dec 2004 11:44:50, seen in news:comp.lang.javascript, Brent
Webster <brent@webstyler.ca> posted :[color=blue]
>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[/color]
// or alert((iYear*100+iMonth)*100+iDay)
// or alert(iYear + '-' + iMonth + '-' + iDay)
[color=blue]
>If there is any way to do this any help would be greatly appreciated.[/color]
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 .
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links. | | | | re: Please Help with Dates
JRS: In article <buRvd.1104$I52.53070@news.optus.net.au>, dated Wed, 15
Dec 2004 07:13:11, seen in news:comp.lang.javascript, RobG
<rgqld@iinet.net.auau> posted :[color=blue]
>
> 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.[/color]
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.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc. |  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|