473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

adding dates to form fields

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?

May 15 '06 #1
14 1923
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)
May 15 '06 #2
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.

May 16 '06 #3
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.

May 16 '06 #4
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.

May 16 '06 #5
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.

May 16 '06 #6
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)
May 16 '06 #7
JRS: In article <11*********************@u72g2000cwu.googlegroups. com>,
dated Tue, 16 May 2006 01:05:48 remote, seen in
news:comp.lang.javascript, gorio <g_*******@yahoo.co.uk> 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.
May 16 '06 #8
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.

May 17 '06 #9
Lee said the following on 5/17/2006 10:59 AM:
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.


Googling for "javascript faq" it is the number 5 hit as well.
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.


Google telling someone to read an FAQ? Surely you jest......

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 17 '06 #10
gorio wrote:
Google search result:
Your search - regularly-posted (except currently) and (still)
frequently-cited newsgroup faq javascript - did not match any documents.


"javascript faq", fifth hit (on the first page, with the Google defaults):

| comp.lang.javascript FAQ - 8.0 - 2004-03-15 - [ ... ]
| You are reading the comp.lang.javascript meta-FAQ, version 8.0 it is
| available on the web at ... This is the official comp.lang.javascript
| (clj) FAQ. ...
| jibbering.com/faq/ - 62k - [...]
PointedEars
--
There are two possibilities: Either we are alone in the
universe or we are not. Both are equally terrifying.
-- Arthur C. Clarke
May 22 '06 #11
gorio wrote:
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.


You should not use a Web browser for accessing Network News at all.
Use a newsreader.
PointedEars
May 22 '06 #12
Thomas 'PointedEars' Lahn said the following on 5/22/2006 12:29 PM:
gorio wrote:
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.


You should not use a Web browser for accessing Network News at all.
Use a newsreader.


People can use whatever they want for reading Usenet, as long as they
understand how to use it.

This is not de.comp.lang.javascript my boy.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 22 '06 #13
Array so: for every value 1st column correspond one in 2nd
2 25
25 27
27 60
4 50
27 61

be able to write this
2 25,27,60,61
4 50

this steps:
-2nd isn't in right then rewrite in 1st column with its value (25)
-the 25 is also in 2nd column, then its value 27 go near 25
.....

some particular code?
Aug 24 '06 #14
padew wrote on 24 aug 2006 in comp.lang.javascript:
Array so: for every value 1st column correspond one in 2nd
Please when starting a new thread, do not include header references to an
older one. Please try to learn how usenet works.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 24 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: netsurfer | last post by:
Hi: Have a question on making the date automatically filled in by what the user enters in by the date at the top. The date entered at the top would most likely be on a Wednesday then I need...
11
by: Bobbak | last post by:
Hello All, I have these tables (lets call it ‘EmpCalls', ‘EmpOrders', and ‘Stats') that each contain the list of EmployeeIDs, I want to be able to create a Module in which I could call in my VB...
5
by: M Skabialka | last post by:
I am creating my first Visual Studio project, an inventory database. I have created a form and used written directions to add data from a table to the form using table adapters, data sets, etc. ...
3
by: myemail.an | last post by:
If I need to format how the content of a field is displayed, I can click ALT + ENTER from design view, and specify the format, for example, the number of decimal digits and so on. Is there a way...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.