Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > adding dates to form fields

Reply
Thread Tools

adding dates to form fields

 
 
gorio
Guest
Posts: n/a
 
      05-15-2006
I have a MySQL/PHP based page with a number of date fields that I would
like the user to be able to run a script on to update the fields and
then submit them for server processing.

All my programming to date has been server side, but I feel this
exercise is better done client side, hence my post on this group.
However my javascript knowledge is very limited.

So far I have something like this, I know it won't work like this but
I've googled out many variations without success. I'm just presenting
it in this very basic script to show what I'm trying to achieve.

function datecalc(form) {
form.date1.value = form.date0.value+12;
form.date2.value = form.date1.value+3;
form.date3.value = form.date2.value+5;
form.date4.value = form.date3.value+7;
form.date5.value = form.date4.value+1
}

i.e. I just want to add 12 days to date0 to give date1, 3 days to date1
to give date2 etc etc.

I feel this should be trivial, but have spent many hours trying to find
the solution without success. Seems to me simply a matter of getting
the syntax right but nothing I've tried seems to work, not even an
error message which makes it that much more difficult.

I know my form & php & basic java function work, because if I make e.g.
the first line simply form.date1.value = form.date0.value then when I
hit the submit button the form value of date1 changes to the value of
date0.

The dates in the form are in yyyy-mm-dd format.

Can someone help with my impending alopecia and show me the way?

 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      05-15-2006
gorio wrote on 15 mei 2006 in comp.lang.javascript:

> I have a MySQL/PHP based page with a number of date fields that I would
> like the user to be able to run a script on to update the fields and
> then submit them for server processing.
>
> All my programming to date has been server side, but I feel this
> exercise is better done client side, hence my post on this group.
> However my javascript knowledge is very limited.
>
> So far I have something like this, I know it won't work like this but
> I've googled out many variations without success. I'm just presenting
> it in this very basic script to show what I'm trying to achieve.
>
> function datecalc(form) {
> form.date1.value = form.date0.value+12;
> form.date2.value = form.date1.value+3;
> form.date3.value = form.date2.value+5;
> form.date4.value = form.date3.value+7;
> form.date5.value = form.date4.value+1
>}
>
> i.e. I just want to add 12 days to date0 to give date1, 3 days to date1
> to give date2 etc etc.


the field valuse are strings, so you are just concatenating a number at
the end of a string.

>
> I feel this should be trivial, but have spent many hours trying to find
> the solution without success. Seems to me simply a matter of getting
> the syntax right but nothing I've tried seems to work, not even an
> error message which makes it that much more difficult.


You will first have to convert the string value to a date object.

> I know my form & php & basic java function work,


This is neither php or java.

Java has nothing to do with javascript but the name.

> because if I make e.g.
> the first line simply form.date1.value = form.date0.value then when I
> hit the submit button the form value of date1 changes to the value of
> date0.
>
> The dates in the form are in yyyy-mm-dd format.
>
> Can someone help with my impending alopecia and show me the way?


===============

function stringDateAdd(x,y){ // expects and returns yyyy-(m)m-(d)d
var d = new Date(x.replace(/-/g,'/'))
d.setDate(d.getDate()+y)
return d.getFullYear()+'-'+(d.getMonth()+1)+'-'+d.getDate()
}

r = stringDateAdd('2006-5-15',12)
alert(r)


r = stringDateAdd('2006-5-30',20)
alert(r)


form.date1.value = stringDateAdd(form.date0.value,12)

==============

As alternative you could try the hat-trick ...
.... for your alopecia



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
 
 
 
gorio
Guest
Posts: n/a
 
      05-16-2006
thanks very much Evertjan, I tried your code and it works very well and
thanks for the bonus alert examples.

One thing which bothers me a bit is the (m)m and (d)d format of the
days and months which is not in keeping with the yyyy-mm-dd format on
the rest of my site. Actually it seems MySQL/PHP doesn't care if the
dates are in yyyy-m-d format since after I update my database the dates
appear formatted correctly as yyyy-mm-dd, but I want to keep
consistency and avoid confusing my users.

I googled and fiddled a bit and found a solution for the months like
this:

function stringDateAdd(x,y){ // expects and returns yyyy-(m)m-(d)d
var d = new Date(x.replace(/-/g,'/'))

var months=new Array(12);
months[0]="01"
months[1]="02"
months[2]="03"
months[3]="04"
months[4]="05"
months[5]="06"
months[6]="07"
months[7]="08"
months[8]="09"
months[9]="10"
months[10]="11"
months[11]="12"
var monthnumber = d.getMonth();
var month_no = months[monthnumber];

d.setDate(d.getDate()+y)
return d.getFullYear()+'-'+month_no+'-'+d.getDate()

}

Is this a reasonable approach? I would have to do the same for the
days this way, but from my research javascript just dishes up the d(d)
m(m) format.

 
Reply With Quote
 
gorio
Guest
Posts: n/a
 
      05-16-2006
thanks very much Evertjan, I tried your code and it works very well and
thanks for the bonus alert examples.

One thing which bothers me a bit is the (m)m and (d)d format of the
days and months which is not in keeping with the yyyy-mm-dd format on
the rest of my site. Actually it seems MySQL/PHP doesn't care if the
dates are in yyyy-m-d format since after I update my database the dates
appear formatted correctly as yyyy-mm-dd, but I want to keep
consistency and avoid confusing my users.

I googled and fiddled a bit and found a solution like this:

function stringDateAdd(x,y){ // expects and returns yyyy-(m)m-(d)d
var d = new Date(x.replace(/-/g,'/'))

d.setDate(d.getDate()+y)

var day_no = (d.getDate())
if (day_no < 10) { day_no = "0" + day_no; }

var month_no = (d.getMonth()+1)
if (month_no < 10) { month_no = "0" + month_no; }

return d.getFullYear()+'-'+month_no+'-'+day_no

}

Is this a reasonable approach? I would have to do the same for the
days this way, but from my research javascript just dishes up the d(d)
m(m) format.

 
Reply With Quote
 
gorio
Guest
Posts: n/a
 
      05-16-2006
hmmm... please ignore the code above, I tried it and found it didn't
work (and in fact didn't think I had posted it at all but somehow my
browser cached it?)

anyhow take two on the date formatting:

I googled and fiddled a bit and found a solution like this:

function stringDateAdd(x,y){ // expects and returns yyyy-(m)m-(d)d
var d = new Date(x.replace(/-/g,'/'))

d.setDate(d.getDate()+y)

var day_no = (d.getDate())
if (day_no < 10) { day_no = "0" + day_no; }

var month_no = (d.getMonth()+1)
if (month_no < 10) { month_no = "0" + month_no; }

return d.getFullYear()+'-'+month_no+'-'+day_no

}

Is this a reasonable approach? I would have to do the same for the
days this way, but from my research javascript just dishes up the d(d)
m(m) format.

 
Reply With Quote
 
gorio
Guest
Posts: n/a
 
      05-16-2006
something screwy going on with my browser here, I didn't mean to post
the 1st reply at all, and in the 2nd reply the only correct part is the
updated code. Must try flushing my cache before posting this.

 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      05-16-2006
gorio wrote on 16 mei 2006 in comp.lang.javascript:

> something screwy going on with my browser here, I didn't mean to post
> the 1st reply at all, and in the 2nd reply the only correct part is the
> updated code. Must try flushing my cache before posting this.
>


Worse is that you do not quote.
This is usenet, not email.
Others could like to understand too.

Please quote what you are replying to. If you want to post a followup via
groups.google.com, don't use the "Reply" link at the bottom of the
article. Click on "show options" at the top of the article, then click on
the "Reply" at the bottom of the article headers.
<http://www.safalra.com/special/googlegroupsreply/>

=============================

> I googled and fiddled a bit and found a solution like this:


This is dangerous, unless you understand the code comleately and
extensive testing has been done. But it should work.

> function stringDateAdd(x,y){ // expects and returns yyyy-(m)m-(d)d
> var d = new Date(x.replace(/-/g,'/'))
>
> d.setDate(d.getDate()+y)
>
> var day_no = (d.getDate())
> if (day_no < 10) { day_no = "0" + day_no; }
>
> var month_no = (d.getMonth()+1)
> if (month_no < 10) { month_no = "0" + month_no; }
>
> return d.getFullYear()+'-'+month_no+'-'+day_no
>
>}


Exterializing repeated action:

========================

function two(x){
return (x<10) ? '0'+x : ''+x ;
};

// expects yyyy-(m)m-(d)d or yyyy-mm-dd and the number of days to add
// returns yyyy-mm-dd

function stringDateAdd(x,y){
var d = new Date(x.replace(/-/g,'/'));
d.setDate(d.getDate()+y);
return d.getFullYear() +
'-' +
two(d.getMonth()+1) +
'-' +
two(d.getDate());
};

========================

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      05-16-2006
JRS: In article <(E-Mail Removed). com>,
dated Tue, 16 May 2006 01:05:48 remote, seen in
news:comp.lang.javascript, gorio <(E-Mail Removed)> posted :
>
>I googled and fiddled a bit and found a solution for the months like
>this:


You should have Googled for the regularly-posted (except currently) and
(still) frequently-cited newsgroup FAQ, which would have given you all
that you need to know, including how to post.

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.
 
Reply With Quote
 
gorio
Guest
Posts: n/a
 
      05-17-2006
Dr John Stockton wrote:
>
> You should have Googled for the regularly-posted (except currently) and
> (still) frequently-cited newsgroup FAQ, which would have given you all
> that you need to know, including how to post.
>

gee thanks doc, your reply reminds me of the hitchhiker's guide the
galaxy where the earthlings are admonished for their failure to read
the intergalactic bulletin regarding the impending demolition of earth,
but I'll remember to Google that one next time...

Google search result:
Your search - regularly-posted (except currently) and (still)
frequently-cited newsgroup faq javascript - did not match any documents.

 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      05-17-2006
gorio said:
>
>Dr John Stockton wrote:
>>
>> You should have Googled for the regularly-posted (except currently) and
>> (still) frequently-cited newsgroup FAQ, which would have given you all
>> that you need to know, including how to post.
>>

>gee thanks doc, your reply reminds me of the hitchhiker's guide the
>galaxy where the earthlings are admonished for their failure to read
>the intergalactic bulletin regarding the impending demolition of earth,
>but I'll remember to Google that one next time...
>
>Google search result:
>Your search - regularly-posted (except currently) and (still)
>frequently-cited newsgroup faq javascript - did not match any documents.



Googling for "comp.lang.javascript faq" finds it immediately.
Whoever gave you access to newsgroups should have asked you to
read some basic instructions, including that you should always
look for a FAQ before posting questions to a newsgroup.


--

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RFC-822 dates into Ruby dates kellygreer1 Ruby 1 06-08-2008 10:59 PM
Need to use dates earlier than 1900 (Time library says out of range for dates < 1900) me@benjaminarai.com Ruby 1 07-17-2007 02:25 PM
FAQ Dates; Opera Dates. Dr J R Stockton Javascript 0 06-18-2007 03:51 PM
Dates dates dates dates... SQL and ASP.NET David Lozzi ASP .Net 1 09-30-2005 02:18 PM
Dates! Dates! Dates! PW ASP General 4 08-09-2004 04:42 PM



Advertisments