本文介绍了在表单字段中添加日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个基于MySQL / PHP的页面,其中包含许多日期字段,我会像用户一样运行脚本来更新字段和 然后提交它们进行服务器处理。 到目前为止我所有的编程都是服务器端,但我觉得这个 运行更好的客户端,因此我在这个小组的帖子。 但是我的javascript知识非常有限。 到目前为止,我有类似的东西,我知道它不会像这样工作但是 我已经搜索了许多变化而没有成功。我只是在这个非常基本的脚本中展示 来展示我想要实现的目标。 函数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 } 即我只想添加12天到date0给date1,3天到期1 给date2等等。 我觉得这应该是微不足道的,但花了很多时间试图找到 解决方案没有成功。对我来说只是一个问题,即语法正确,但我尝试过的任何东西似乎都没有用,甚至没有一个 错误消息,这使得它变得更加困难。 我知道我的表格& php&基本的java函数工作,因为如果我制作例如 的第一行只是form.date1.value = form.date0.value然后当我 点击提交按钮时表格date1的值更改为 date0的值。 表单中的日期格式为yyyy-mm-dd格式。 有人可以帮助解决我即将发生的脱发问题,并告诉我方向吗? 解决方案 gorio在15 mei 2006上写道在comp.lang.javascript : 我有一个基于MySQL / PHP的页面,其中包含许多日期字段,我会像用户一样能够运行脚本来更新字段,然后提交它们进行服务器处理。 我迄今为止所有的编程都是服务器端,但我觉得这个运动更好地完成客户端,因此我在这个小组的帖子。然而我的javascript知识非常有限。 到目前为止,我有类似的东西,我知道它赢了'这样的工作但是我已经搜索了许多变化而没有成功。我只是在这个非常基本的脚本中展示它,以显示我想要实现的目标。 函数datecalc(form){ form.date1.value = form.date0.value + 12; form.date2.value = form.date1.value + 3; form.date3.value = form.date2.value + 5; 表格.date4.value = form.date3.value + 7; form.date5.value = form.date4.value + 1 } 即我只想添加12到目前为止0给日期1,给出日期3天1 给出date2等等。 字段值是字符串,所以你只是在 字符串的结尾。 我觉得这应该是微不足道的,但是花了很多时间试图找到解决方案而没有成功。在我看来只是一个正确的语法问题,但我尝试过的任何东西似乎都没有用,甚至没有一个错误信息让它变得更加困难。 您首先必须将字符串值转换为日期对象。 我知道我的表格& php&基本的java函数工作, 这不是php或java。 Java与javascript无关,而是名称。 因为如果我第一行只是form.date1.value = form.date0.value,那么当我点击提交按钮时,date1的表格值会变为值 date0。 表格中的日期是yyyy-mm-dd格式。 有人可以帮我解决即将发生的脱发问题吗? / blockquote> =============== 函数stringDateAdd(x,y){ //期望并返回yyyy-(m)m-(d)d var d =新日期(x.replace(/ - / g,''/'')) d.setDate(d.getDate()+ y) 返回d.getFullYear()+'' - ''+(d.getMonth()+ 1)+'' - ''+ d.getDate() } r = stringDateAdd(''2006-5-15'',12) alert(r) r = stringDateAdd(''2006-5-30'',20) alert(r) 表格.date1.value = st ringDateAdd(form.date0.value,12) ============== 作为替代你可以试试帽子戏法...... ....为你的脱发 - Evertjan。 荷兰。 (请在我的电子邮件地址中将x'变为点数) 非常感谢Evertjan,我尝试了你的代码并且效果非常好并且 感谢奖励提示示例。 让我困扰的一件事是(m )m和(d)格式的 天和月不符合我的网站其余部分的yyyy-mm-dd格式。实际上似乎MySQL / PHP并不关心 日期是否为yyyy-md格式,因为在我更新数据库后,日期 看起来格式正确为yyyy- mm-dd,但我想保持 一致性并避免让我的用户感到困惑。 我用google搜索并稍稍摆弄并找到了几个月的解决方案 这个: 函数stringDateAdd(x,y){//期望并返回yyyy-(m)m-(d)d var d =新日期(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" 个月[9] =" 10" 个月[10] =" 11" 个月[11] =" 12" ; v ar monthnumber = d.getMonth(); var month_no = months [monthnumber]; d.setDate(d.getDate()+ y) 返回d.getFullYear()+'' - ''+ month_no +'' - ''+ d.getDate() } 这是一种合理的方法吗?我必须以这种方式为 天做同样的事情,但是从我的研究javascript开始就是d(d) m(m)格式。 非常感谢Evertjan,我尝试了你的代码,它运行得非常好并且 感谢奖励提示示例。 让我烦恼的一件事是 天和月的(m)m和(d)d格式与yyyy-mm不一致-dd格式 我的网站的其余部分。实际上似乎MySQL / PHP并不关心 日期是否为yyyy-md格式,因为在我更新数据库后,日期 看起来格式正确为yyyy- mm-dd,但我想保持 的一致性,避免让我的用户感到困惑。 我用谷歌搜索了一下,找到了这样的解决方案: 函数stringDateAdd(x,y){//期望并返回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; } 返回d.getFullYear()+'' - ''+ month_no +'' - ''+ day_no } 这是一种合理的方法吗?我必须以这种方式为 天做同样的事情,但是从我的研究javascript开始就是d(d) m(m)格式。 I have a MySQL/PHP based page with a number of date fields that I wouldlike the user to be able to run a script on to update the fields andthen submit them for server processing.All my programming to date has been server side, but I feel thisexercise 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 butI''ve googled out many variations without success. I''m just presentingit 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 date1to give date2 etc etc.I feel this should be trivial, but have spent many hours trying to findthe solution without success. Seems to me simply a matter of gettingthe syntax right but nothing I''ve tried seems to work, not even anerror 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 Ihit the submit button the form value of date1 changes to the value ofdate0.The dates in the form are in yyyy-mm-dd format.Can someone help with my impending alopecia and show me the way? 解决方案 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 atthe 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)dvar 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)thanks very much Evertjan, I tried your code and it works very well andthanks for the bonus alert examples.One thing which bothers me a bit is the (m)m and (d)d format of thedays and months which is not in keeping with the yyyy-mm-dd format onthe rest of my site. Actually it seems MySQL/PHP doesn''t care if thedates are in yyyy-m-d format since after I update my database the datesappear formatted correctly as yyyy-mm-dd, but I want to keepconsistency and avoid confusing my users.I googled and fiddled a bit and found a solution for the months likethis:function stringDateAdd(x,y){ // expects and returns yyyy-(m)m-(d)dvar 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 thedays this way, but from my research javascript just dishes up the d(d)m(m) format. thanks very much Evertjan, I tried your code and it works very well andthanks for the bonus alert examples.One thing which bothers me a bit is the (m)m and (d)d format of thedays and months which is not in keeping with the yyyy-mm-dd format onthe rest of my site. Actually it seems MySQL/PHP doesn''t care if thedates are in yyyy-m-d format since after I update my database the datesappear formatted correctly as yyyy-mm-dd, but I want to keepconsistency 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)dvar 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 thedays this way, but from my research javascript just dishes up the d(d)m(m) format. 这篇关于在表单字段中添加日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 11:15