< script language =" JavaScript"> var daym = addZ(mydate.getDate()); 和以下''if''语句是多余的。 var week1array = new Array (No Meeting,Surrey,Cloverdale,No Meeting,Langley,No Meeting,No Meeting) 您可能会发现维护更容易初始化数组: var week1array = [ No Meeting, Surrey, Cloverdale, No Meeting, Langley, 没有会议, 没有会议 ] 也许它已经生成了代码所以没关系... va r week2array = new Array(No Meeting,No Meeting,Whiterock,Abbotsford,Hope,No Meeting,No Meeting) if((daymtom)< 8) {document.write("< small>< font face =''Arial''>< b> ;" + week1array [tom] +"< / b>< / font>< / small>")} if if((daymtom)< 15) {document .write("< small>< font face =''Arial''>< b>" + week2array [tom] +"< / b>< / font>< ; / small>")} if if((daymtom)< 22) {document.write("< small>< font face =''Arial' '>< b>" + week1array [tom] +"< / b>< / font>< / small>")} if if((daymtom)< 29) {document.write("< small>< font face =''Arial''>< b>" + week2array [tom] +"< / b> < / font>< / small>")} else {document.write(" No Meeting")} 会议从未在29日,30日或31日召开? } 有多种规则可以确定你/ b 的月哪个星期,确保用户了解你的算法是什么正在使用。 在我看来你的周数由下式给出: var weekNum = Math.floor((daymtom-1)/ 7)+ 1 ; 你对28岁以上的日期做了什么? [...] - Rob RobG写道: [snip] 你可能想用添加前导零的函数: 函数addZ(n)返回(x } 确保n总是以字符串形式返回(如果这是你想要的)。 函数addZ(n) { return(n< 10)? ''0''+ n:''''+ n; } Jambalaya写道:函数addZ(n) {返回(n< 10)? ''0''+ n:''''+ n; } 压缩,娱乐: 函数addZ(n){return(n< 10&&''0'')+ n} - Matt Kruse http://www.JavascriptToolbox.com http://www.AjaxToolbox.com I have a page which lists of locations of meetings that occur on specificdays of the week (every 1st and 3rd Thursday the meeting is in Langley - asan example)The following code works for when it is the day of the week, but I have aproblem when I list tomorrows meeting and today''s date is the last day ofthe week - it will not recognise the 1st day of the next moth as beingtomorrows date.I know this is probably very crude, but this is what I have been using - anyideas???<script language="JavaScript"><!--function tomorrow() {var mydate=new Date()var day=mydate.getDay()var daym=mydate.getDate()if (daym<10)daym="0"+daymvar tom=day+1var daymtom=daym+1var week1array=new Array("No Meeting","Surrey","Cloverdale","NoMeeting","Langley","No Meeting","No Meeting")var week2array=new Array("No Meeting","NoMeeting","Whiterock","Abbotsford","Hope","No Meeting","No Meeting")if ((daymtom)<8){document.write("<small><fontface=''Arial''><b>"+week1array[tom]+"</b></font></small>")}else if ((daymtom)<15){document.write("<small><fontface=''Arial''><b>"+week2array[tom]+"</b></font></small>")}else if ((daymtom)<22){document.write("<small><fontface=''Arial''><b>"+week1array[tom]+"</b></font></small>")}else if ((daymtom)<29){document.write("<small><fontface=''Arial''><b>"+week2array[tom]+"</b></font></small>")}else{document.write("No Meeting")}}// --></script> 解决方案 Prophet wrote: I have a page which lists of locations of meetings that occur on specific days of the week (every 1st and 3rd Thursday the meeting is in Langley - as an example) The following code works for when it is the day of the week, but I have a problem when I list tomorrows meeting and today''s date is the last day of the week - it will not recognise the 1st day of the next moth as being tomorrows date.That is because you get today''s date, then if it''s less than 10 youconvert it to a a string, otherwise you keep it as a number. I know this is probably very crude, but this is what I have been using - any ideas??? <script language="JavaScript">The language attribute is deprecated, type is required:<script type="text/javascript"> <!--HTML comment delimiters inside script elements serve no useful purposeand are potentially harmful - just don''t use them. function tomorrow() { var mydate=new Date() var day=mydate.getDay() var daym=mydate.getDate() if (daym<10) daym="0"+daym var tom=day+1If tom should be the day number for tomorrow:var tom = (day + 1)%7; var daymtom=daym+1If daymtom should be tomorrow''s date:mydate.setDate(mydate.getDate() + 1);var daymtom = mydate.getDate();mydate has now been set to tomorrow, but you don''t use it anymore soit should be OK.You may want to use a function to add the leading zero:function addZ(n){return (x<10)? ''0''+x : ''''+x;}Ensures n is always returned as a string (if that''s what you want).Now the line:var daym=mydate.getDate()becomes:var daym = addZ(mydate.getDate());and the following ''if'' statement is redundant. var week1array=new Array("No Meeting","Surrey","Cloverdale","No Meeting","Langley","No Meeting","No Meeting")You may find it easier for maintenance to initialise arrays:var week1array = ["No Meeting","Surrey","Cloverdale","No Meeting","Langley","No Meeting","No Meeting"]Maybe it''s generated code so it doesn''t matter... var week2array=new Array("No Meeting","No Meeting","Whiterock","Abbotsford","Hope","No Meeting","No Meeting") if ((daymtom)<8) {document.write("<small><font face=''Arial''><b>"+week1array[tom]+"</b></font></small>")} else if ((daymtom)<15) {document.write("<small><font face=''Arial''><b>"+week2array[tom]+"</b></font></small>")} else if ((daymtom)<22) {document.write("<small><font face=''Arial''><b>"+week1array[tom]+"</b></font></small>")} else if ((daymtom)<29) {document.write("<small><font face=''Arial''><b>"+week2array[tom]+"</b></font></small>")} else {document.write("No Meeting")}Are meetings never held on the 29th, 30th or 31st? }There are a variety of rules to determine which week of the month youare in, make sure users understand what algorithm you are using.Seems to me your week number is given by:var weekNum = Math.floor((daymtom-1)/7) + 1;What do you do for dates beyond 28?[...]--RobRobG wrote:[snip] You may want to use a function to add the leading zero: function addZ(n) { return (x<10)? ''0''+x : ''''+x; } Ensures n is always returned as a string (if that''s what you want).function addZ(n){return (n<10) ? ''0''+n : ''''+n;} Jambalaya wrote: function addZ(n) { return (n<10) ? ''0''+n : ''''+n; }compacting, for amusement:function addZ(n){return (n<10&&''0'')+n}--Matt Kruse http://www.JavascriptToolbox.com http://www.AjaxToolbox.com 这篇关于一个月的一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-29 03:14