469,328 Members | 1,229 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

Date handling

We have some date fields in our HTML forms in various places. In the
accounts package we use, there is some pretty nifty date handling.

It has a standardised date format of dd/mm/yyyy and if you enter e.g.

3-12 it will switch it to 3/12/2005

Same applies for e.g. 3:12 etc

Anyone know of some javascript which will do this for our HTML form
fields? Our target format is actually dd-mon-yyyy and it would make it
really nice for users if the system could accept a variety of input
formats and then switch it to a date to our specification. Any
suggestions?
--

jeremy
Oct 25 '05 #1
8 3966
Jeremy wrote:
We have some date fields in our HTML forms in various places. In the
accounts package we use, there is some pretty nifty date handling.

It has a standardised date format of dd/mm/yyyy and if you enter e.g.

3-12 it will switch it to 3/12/2005

Same applies for e.g. 3:12 etc

Anyone know of some javascript which will do this for our HTML form
fields? Our target format is actually dd-mon-yyyy and it would make it
really nice for users if the system could accept a variety of input
formats and then switch it to a date to our specification. Any
suggestions?


The task is trivial, but first you need to ensure that your users can
not introduce ambiguities.
A sample:
http://www.mickweb.com/demo/datepicker.html
Mick


Oct 25 '05 #2
In article <j6*******************@twister.nyroc.rr.com>, Mick White
says...
Jeremy wrote:
We have some date fields in our HTML forms in various places. In the
accounts package we use, there is some pretty nifty date handling.

It has a standardised date format of dd/mm/yyyy and if you enter e.g.

3-12 it will switch it to 3/12/2005

Same applies for e.g. 3:12 etc

Anyone know of some javascript which will do this for our HTML form
fields? Our target format is actually dd-mon-yyyy and it would make it
really nice for users if the system could accept a variety of input
formats and then switch it to a date to our specification. Any
suggestions?


The task is trivial, but first you need to ensure that your users can
not introduce ambiguities.
A sample:
http://www.mickweb.com/demo/datepicker.html

Thanks Mick, perhaps I didn't make myself clear. From what I see, you
are enabling the user to select a date from 3 individual fields and then
displaying the resultinto a 4th field

What I am looking to acheive is a single text field in which the user
may type e.g. 3/12/05 and which, as the cursor leaves the field, is
reformatted to 3-dec-2005.

You said it is trivial: is it really?

thanks

--

jeremy

Oct 25 '05 #3
JRS: In article <MP************************@news.individual.net> , dated
Tue, 25 Oct 2005 15:32:24, seen in news:comp.lang.javascript, Jeremy
<je********@gmail.com> posted :
We have some date fields in our HTML forms in various places. In the
accounts package we use, there is some pretty nifty date handling.

It has a standardised date format of dd/mm/yyyy and if you enter e.g.

3-12 it will switch it to 3/12/2005

Same applies for e.g. 3:12 etc

Anyone know of some javascript which will do this for our HTML form
fields? Our target format is actually dd-mon-yyyy and it would make it
really nice for users if the system could accept a variety of input
formats and then switch it to a date to our specification. Any
suggestions?


If you allow an adaptive variety, you need to be reasonably sure that
there are no unexpected cases that are misinterpreted, because your
users may not notice that the switched form is not as expected.

Better IMHO to provide a set of radio-buttons and/or checkboxes to
choose the current format, and to display by onChange or similar the
date of Christmas in the currently selected format.

Do you go as far as allowing 25.XII.2005, 33/11/22, and 2005-W51-7, for
example?

You could try reading the newsgroup FAQ; see below.

--
© 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.
Oct 25 '05 #4
Jeremy wrote:



Thanks Mick, perhaps I didn't make myself clear. From what I see, you
are enabling the user to select a date from 3 individual fields and then
displaying the resultinto a 4th field

What I am looking to acheive is a single text field in which the user
may type e.g. 3/12/05 and which, as the cursor leaves the field, is
reformatted to 3-dec-2005.

You said it is trivial: is it really?


Yes, but you can never be sure what the user will enter.

function convertDate(value){
var v=value.split(/[^\d]/);
v[1]=
['jan','feb','mar','apr','may','jun','jul','aug','s ep','oct','nov','dec'][v[1]-1];
return v.join("-");
}

alert("3/12/05") // "3-dec-2005"
<input ... onchange="this.value=convertDate(this.value)">

You're aware that "3/12/2005" is interpreted as "March 12, 2005" in
North America, are you not?
Mick.

Oct 25 '05 #5
Jeremy wrote:
It has a standardised date format of dd/mm/yyyy and if you enter e.g.
3-12 it will switch it to 3/12/2005
Same applies for e.g. 3:12 etc
Anyone know of some javascript which will do this for our HTML form
fields?


Using my generalized date functions at
http://www.javascripttoolbox.com/date/
you could do something like this:

<input type="text" name="date"
onChange="var d=parseDate(this.value);if(d==null){alert('Date format not
recognized!');}else{this.value=formatDate(d,'dd-MMM-yyyy)}">

With the default code, it would automatically recognize and parse dates in
these formats:
y-M-d MMM d, y MMM d,y y-MMM-d d-MMM-y MMM d
M/d/y M-d-y M.d.y MMM-d M/d M-d
d/M/y d-M-y d.M.y d-MMM d/M d-M

You could modify the parseDate function to easily parse more date formats.

You should of course be aware that some values entered will be ambiguous.
1/2/2000 can mean Jan 2, 2000 just as well as Feb 1, 2000, depending on
locale.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Oct 26 '05 #6
In article <tR**************@merlyn.demon.co.uk>, Dr John Stockton
says...

If you allow an adaptive variety, you need to be reasonably sure that
there are no unexpected cases that are misinterpreted, because your
users may not notice that the switched form is not as expected.
Yes you are quite right. This is however an administrative application
as opposed to something that any member of the public may use and hence
training etc on the behaviour of the system will be available - and they
will know that the target date format is dd-mon-yyyy.
Better IMHO to provide a set of radio-buttons and/or checkboxes to
choose the current format, and to display by onChange or similar the
date of Christmas in the currently selected format.

Do you go as far as allowing 25.XII.2005, 33/11/22, and 2005-W51-7, for
example?

You could try reading the newsgroup FAQ; see below.


Thanks for the pointer.
--

jeremy
Oct 26 '05 #7
In article <YZ*******************@twister.nyroc.rr.com>, Mick White
says...
You said it is trivial: is it really?

Yes, but you can never be sure what the user will enter.

function convertDate(value){
var v=value.split(/[^\d]/);
v[1]=
['jan','feb','mar','apr','may','jun','jul','aug','s ep','oct','nov','dec'][v[1]-1];
return v.join("-");
}

alert("3/12/05") // "3-dec-2005"
<input ... onchange="this.value=convertDate(this.value)">


Thanks, that's very helpful.
You're aware that "3/12/2005" is interpreted as "March 12, 2005" in
North America, are you not?


Noted ta.
--

jeremy
Oct 26 '05 #8
In article <dj*********@news4.newsguy.com>, Matt Kruse says...
Jeremy wrote:
It has a standardised date format of dd/mm/yyyy and if you enter e.g.
3-12 it will switch it to 3/12/2005
Same applies for e.g. 3:12 etc
Anyone know of some javascript which will do this for our HTML form
fields?
Using my generalized date functions at
http://www.javascripttoolbox.com/date/
you could do something like this:

<input type="text" name="date"
onChange="var d=parseDate(this.value);if(d==null){alert('Date format not
recognized!');}else{this.value=formatDate(d,'dd-MMM-yyyy)}">

With the default code, it would automatically recognize and parse dates in
these formats:
y-M-d MMM d, y MMM d,y y-MMM-d d-MMM-y MMM d
M/d/y M-d-y M.d.y MMM-d M/d M-d
d/M/y d-M-y d.M.y d-MMM d/M d-M

You could modify the parseDate function to easily parse more date formats.


OK that looks interesting too.

You should of course be aware that some values entered will be ambiguous.
1/2/2000 can mean Jan 2, 2000 just as well as Feb 1, 2000, depending on
locale.


Thanks for the reminder.
--

jeremy

Oct 26 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Sune | last post: by
3 posts views Thread by Dr John Stockton | last post: by
3 posts views Thread by bhavu10 | last post: by
10 posts views Thread by ashore | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.