计数脚本有问题 [英] Problem with count-up script

查看:63
本文介绍了计数脚本有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法让以下脚本*几乎*工作。但是,它b / b
仍然有计算月数的问题。我试图计算的日期是1994年10月15日。使用正确的thatmonth

(10)它显示0.用9显示2,而不是1,这将是

正确。


有什么建议吗?


===剪切===

< script language = Javascript type = text / javascript class =" smalltext">

/ *


日期点票1.0

(C)版权所有1996 Ben Harold

保留所有权利

随意在您的页面中使用此脚本

条件:

1.不要以任何方式修改此脚本(除了

配置说明后的

)未经我的同意

2.通过 bh*****@indyunix.iupui.edu给我发邮件如果你使用它

3.我不对这个

脚本可能对你的计算机做什么的b $ b负责

* /


// Configur方向

//不要改变这个

//这会产生一个用于获取当前日期的日期变量


今天=新日期()


//不要改变这些

//这些获得当前的年,月和日


var thisyear = today.getFullYear()

var thismonth = today.getMonth()

var thisdate = today.getDate( )


//更改这些

//这些设置年份,月份和日期来计算

//注意:var thatmonth应该是0到11之间的数字,而不是1

和12


var thatyear = 1994

var thatmonth = 9

var thatdate = 15


//更改此

//这是浏览器在此之前显示的内容年,月,

和日期

//注意:确保在var的最后一个单词之后有空格

prestring


var prestring ="


//不要改变这些

//剧本其他部分使用的这些设定变量


var fromyears =(thisyear - thatyear)

var datenumber =(thisdate + thatdate)


//不要改变这个

//这表示当月有多少天

如果(thismonth == 0)


monthdates =( 31)


否则如果(thismonth == 1)


monthdates =(28)


否则(thismonth == 2)


monthdates =(31)


else if(thismonth == 3)


monthdates =(30)


否则如果(thismonth == 4)


monthdates =(31)


否则如果(thismonth == 5)


monthdates =(30)


else如果(thismonth == 6)


monthdates =(31)


否则如果(thismonth == 7)


monthdates =(31)


else if(thismon th == 8)


monthdates =(30)


else if(thismonth == 9)


monthdates =(31)


否则如果(thismonth == 10)


monthdates =(30)


否则如果(thismonth == 11)


monthdates =(31)


// Don''改变这个

//这就算出了自那年以来已经过了多少年

if(fromyears == 0)


yearssince =(prestring)


else if(fromyears == 1)


yearssince =(prestring +"年)


else yearssince =(prestring + fromyears +" years")

//不要改变这个

//这可以计算自该日期以来已经过多少日期

if(thisdate> thatdate)


predatessince =(thisdate - thatdate)


else predatessince =(thisdate + monthdates - thatdate)


if(predatessince == 0)


datessince =(" no days。")

else if(predatessince == 1)


datessince =(" 1天。)


else datessince =(predatessince +" days。")


//不要改变这个

//这就算出了自那个月以来已经过了几个月

if(thisyear> thatyear){


if(thismonth> = thatmonth)


premonthssince =(thismonth -

thatmonth)


else premonthssince = (12 + thismonth -

thatmonth)

}


else premonthssince =(thismonth - thatmonth)


if(monthdates< datebumber)


premonthssincetwo =(premonthssince + 1)


else premonthssincetwo =(premonthssince)


if(premonthssincetwo == 0)

monthssince =("")


else if(premonthssincetwo == 1)


monthssince =(0个月)


else monthssince =(premonthssincetwo +

" months")

//不要改变这些

//这些弄清楚在最终的

消息中使用什么类型的标点符号

if(yearssince == prestring)


commaone =("")


else {


if(monthssince =="")


(commaone ="和)


else commaone =(",")


}

if(commaone ==" and)

>
commatwo =("")


else if(commaone ==",")


commatwo =("和)


else if(yearssince == prestring){


if(monthssince =="")


(commatwo ="")

其他commatwo =("和)


}

//不要改变这个

//汇总最后的消息

var finalstring =""


finalstring + =(yearssince)


finalstring + =(commaone)


finalstring + =( $)
finalstring + =(commatwo)


finalstring + =(datessince)

// Don'不要改变这个

//这会将最终消息打印到浏览器屏幕

document.write(finalstring)


< ; / script>

===削减===

Kari Suomela


KARICO商业服务

加拿大多伦多
http://www.karico.ca


...接下来 - 谴责母性和苹果派 - 杰拉尔多!

---- ==通过Newsfeeds.Com发布 - 无限制 - 无限制 - 安全使用网新闻== ----
http://www.newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组

---- =东海岸和西海岸服务器农场 - 通过加密实现全隐私= ----

I have managed to get the below script *almost* working. However, it
still has a problem calculating the number of months. The date I am
trying to calculate from is Oct 15, 1994. With the correct "thatmonth"
(10) it displays 0. With 9 it displays 2, instead of 1 which would be
correct.

Any suggestions?

=== Cut ===
<script language=Javascript type=text/javascript class="smalltext">
/*

Date Count-up 1.0
(C) Copyright 1996 Ben Harold
All rights Reserved
Feel free to use this script in your page under the folling
conditions :
1. Do not modify this script in any way (besides
following the
configuration directions) without my consent
2. Mail me at bh*****@indyunix.iupui.edu if you use it
3. I am not held responsible for any thing that this
script may
do to your computer
*/

// Configuration Directions
// Don''t change this
// This makes a date variable that is used to get the current date

today = new Date()

// Don''t change these
// These get the current year, month, and date

var thisyear = today.getFullYear()
var thismonth = today.getMonth()
var thisdate = today.getDate()

// Change these
// These set the year, month, and date to count from
// NOTICE : var thatmonth should be a number between 0 and 11, not 1
and 12

var thatyear = 1994
var thatmonth = 9
var thatdate = 15

// Change this
// This is what the browser will display just before the years, months,
and dates
// NOTICE : make sure that there is a space after the last word of var
prestring

var prestring = " "

// Don''t change these
// These set variables used by other parts of the script

var fromyears = (thisyear - thatyear)
var datenumber = (thisdate + thatdate)

// Don''t change this
// This figures out how many days there are in the current month
if (thismonth == 0)

monthdates = (31)

else if (thismonth == 1)

monthdates = (28)

else if (thismonth == 2)

monthdates = (31)

else if (thismonth == 3)

monthdates = (30)

else if (thismonth == 4)

monthdates = (31)

else if (thismonth == 5)

monthdates = (30)

else if (thismonth == 6)

monthdates = (31)

else if (thismonth == 7)

monthdates = (31)

else if (thismonth == 8)

monthdates = (30)

else if (thismonth == 9)

monthdates = (31)

else if (thismonth == 10)

monthdates = (30)

else if (thismonth == 11)

monthdates = (31)

// Don''t change this
// This figures out how many years it has been since thatyear
if (fromyears == 0)

yearssince = (prestring)

else if (fromyears == 1)

yearssince = (prestring + " year")

else yearssince = (prestring + fromyears + " years")
// Don''t change this
// This figures out how many dates it has been since thatdate
if (thisdate > thatdate)

predatessince = (thisdate - thatdate)

else predatessince = (thisdate + monthdates - thatdate)

if (predatessince == 0)

datessince = ("no days.")

else if (predatessince == 1)

datessince = ("1 day.")

else datessince = (predatessince + " days.")

// Don''t change this
// This figures out how many months it has been since thatmonth
if (thisyear > thatyear) {

if (thismonth >= thatmonth)

premonthssince = (thismonth -
thatmonth)

else premonthssince = (12 + thismonth -
thatmonth)

}

else premonthssince = (thismonth - thatmonth)

if (monthdates < datenumber)

premonthssincetwo = (premonthssince + 1)

else premonthssincetwo = (premonthssince)

if (premonthssincetwo == 0)

monthssince = (" ")

else if (premonthssincetwo == 1)

monthssince = ("0 months")

else monthssince = (premonthssincetwo +
" months")
// Don''t change these
// These figure out what type of punctuation to use in the final
message
if (yearssince == prestring)

commaone = (" ")

else {

if (monthssince == " ")

(commaone = " and ")

else commaone = (", ")

}
if (commaone == " and ")

commatwo = (" ")

else if (commaone == ", ")

commatwo = (" and ")

else if (yearssince == prestring) {

if (monthssince == " ")

(commatwo = " ")

else commatwo = (" and ")

}
// Don''t change this
// This assembles the final message
var finalstring = ""

finalstring += (yearssince)

finalstring += (commaone)

finalstring += (monthssince)

finalstring += (commatwo)

finalstring += (datessince)
// Don''t change this
// This prints the final message to the browser screen
document.write(finalstring)

</script>
=== Cut ===
Kari Suomela

KARICO Business Services
Toronto, ON Canada
http://www.karico.ca

.... Next - condemning motherhood and apple pie - Geraldo!
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

推荐答案


no**@of.your.biz.nes 写道:
我设法得到以下脚本*几乎*工作。但是,它仍然有计算月数的问题。我试图计算的日期是1994年10月15日。使用正确的thatmonth
(10)它显示0.用9表示显示2,而不是1表示
正确。
I have managed to get the below script *almost* working. However, it
still has a problem calculating the number of months. The date I am
trying to calculate from is Oct 15, 1994. With the correct "thatmonth"
(10) it displays 0. With 9 it displays 2, instead of 1 which would be
correct.




对不起,我试着帮忙大约10分钟,但这个脚本是如此

很奇怪,我只是不能再跟着了。这就像他想要的那样?
Kill The Programming自己编写,我只是觉得有些邪恶的力量

comimg from the lines :-)

抱歉,抱歉,抱歉,如果这是你的剧本。


在我眼前的黑暗中我注意到了两个错误:


[1]据推测,2月仅包含28天。它不是完全正确的
。它包含正常年份的28天和29天的所谓

称为闰年。闰年的常用公式(会计也是经常被遗忘的b $ b,400上的奇数规则)如果闰年则返回true,false

否则:


函数isLeap(年){

if((年%400 == 0)||((年%4 == 0)&&(年%100) != 0))){return true;}

else {return false;}}


[2] daythis + daythat可能会给你一个有趣的结果比方说15和

17(10月32日左右)。



I''m sorry, I tried to help for around 10 mins but this scripting is so
strange, I just couldn''t follow it anymore. It''s like Him Who Want To
Kill The Programming wrote it himself, I just feel some evil force
comimg out from the lines :-)
Sorry, sorry, sorry if it''s your script.

Two errors I managed to notice before the darkness in my eyes:

[1] It is presumed that February contains 28 days only. It is not
exactly true. It contains 28 days in regular year and 29 days in so
called leap year. The common formula for leap year (accounting also the
often forgotten "odd on 400" rule) returns true if leap year, false
otherwise:

function isLeap(year) {
if ((year%400==0)||((year%4==0)&&(year%100!=0))) {return true;}
else {return false;}}

[2] daythis+daythat may give you an interesting result for say 15 and
17 (32 October or so).


no ** @ of.your.biz.nes 写道:
我设法得到以下脚本*几乎*工作。但是,它仍然有计算月数的问题。我试图计算的日期是1994年10月15日。使用正确的thatmonth
(10)它显示0.用9表示显示2,而不是1表示
正确。

有什么建议吗?


你好像已经决定以最长的方式解决这个问题。

如果你打算计算自那以来的天数,月数和年数

1994年10月15日(某人的生日?)然后还有更多的简单方法。


你会在这里找到很多关于日期算术的东西:


< URL:http://www.merlyn.demon.co.uk/js-dates.htm>


===剪切===
< script language = Javascript type = text / javascript class =" smalltext">


给脚本元素一个类似乎没什么意义。不推荐使用

语言属性,保持类型,即使不需要,也可以引用属性值,这总是一个很好的想法:


< script type =" text / javascript">

日期计数1.0
(C)版权所有1996 Ben Harold


我看起来和其他地方看起来有类似的东西,我不知道这是不是很好。但是这个算法同样长而痛苦。


[...]

今天=新日期()


创建一个全局变量没有必要。保持

本地的''var'':


var today = new Date();

var thisyear = today .getFullYear()
var thismonth = today.getMonth()
var thisdate = today.getDate()


语句应以分号结尾。翻译将

将它们插入它认为应该去的地方,但为什么要把它留给它?


var thisyear = today.getFullYear();

var thismonth = today.getMonth();

var thisdate = today.getDate();

var thatyear = 1994
var thatmonth = 9
var thatdate = 15
var prestring ="

var fromyears =(thisyear - thatyear)
var datenumber =(thisdate + thatdate)

//不要改变这个
//这表示当月有多少天
I have managed to get the below script *almost* working. However, it
still has a problem calculating the number of months. The date I am
trying to calculate from is Oct 15, 1994. With the correct "thatmonth"
(10) it displays 0. With 9 it displays 2, instead of 1 which would be
correct.

Any suggestions?
You seem to have determined to go about this the longest way possible.
If your intention is to count the number of days, months and years since
15 October, 1994 (someone''s birthday?) then there are much more
concise ways of doing it.

You will find lots of stuff about date arithmetic here:

<URL:http://www.merlyn.demon.co.uk/js-dates.htm>


=== Cut ===
<script language=Javascript type=text/javascript class="smalltext">
There seems little point in giving a script element a class. The
language attribute is deprecated, keep type, and it is always a good
idea to quote attribute values even when not needed:

<script type="text/javascript">


Date Count-up 1.0
(C) Copyright 1996 Ben Harold
I''ve seem similar stuff to this posted elsewhere, I don''t know if it was
Ben''s work but the algorithm was just as long and painful.

[...]

today = new Date()
That creates a global variable which is just not necessary. Keep it
local with ''var'':

var today = new Date();
var thisyear = today.getFullYear()
var thismonth = today.getMonth()
var thisdate = today.getDate()
Statements should be terminated with a semi-colon. The interpreter will
insert them where it thinks they should go, but why leave it to chance?

var thisyear = today.getFullYear();
var thismonth = today.getMonth();
var thisdate = today.getDate();
var thatyear = 1994
var thatmonth = 9
var thatdate = 15
var prestring = " "
var fromyears = (thisyear - thatyear)
var datenumber = (thisdate + thatdate)

// Don''t change this
// This figures out how many days there are in the current month



一个更简洁的例程是将日期对象的日期设置为第0个下个月
,然后得到它的日期。由于本月和本年你已经拥有

的数量:


var monthdates = new Date(thisyear,(thismonth + 1),0)。 getDate();

你已经完成了。


但是这不是一塌糊涂,而是这样做的例行程序将会做你所做的事情。 />
想要,它不是速度的最佳选择,需要''fromDate''比''toDate'更早。

'。夏令时不应该是一个问题,如果你的系统日期是错误的......好吧,我不能从这里解决这个问题。 :-)


它也可以更简洁,但它应该可以完成这项工作。

//传递格式为YYYY-MM-DD的日期字符串,

//例如''1994-10-15''

函数dateDifYMD(ds){

//创建日期对象 - 来自必须更早的日期

var toDate = new Date();

var dsA = ds.split('' - '');

var fromDate = new Date(+ dsA [ 0],+ dsA [1] -1,+ dsA [2]);


//获取两个日期的月数

var toMonths = toDate .getFullYear()* 12 + toDate.getMonth();

var fromMonths = fromDate.getFullYear()* 12 + fromDate.getMonth();


//在几个月内获得好处

var months = toMonths - fromMonths;


//从月份减去月份,如果走得太远则修复

toDate.setMonth(toDate.getMonth() - months);

if(toDate< fromDate){

toDate.setMonth(toDate.getMonth() +1);

- 月;

}

//计算整年和月数

var years = Math.floor(月/ 12);

个月=月%12; //或月数=月数 - 年* 12;


//逐个减去天数,直到走得太远

var days = 0;

while(toDate> fromDate){

toDate.setDate(toDate.getDate() - 1);

++天;

}

//调整太远

--days;


//格式返回字符串

var rString = years +''year''+((years!= 1)?'s'':'''')+'',''

+月+''月''+((月!= 1)?'''':'''')+'',''

+天+''天' '+((days!= 1)?'s'':'''');


返回rString;

}


alert(dateDifYMD('''1994-10-15''));

[...]

-

Rob



A much more concise routine is to set a date of a date object to the 0th
of next month, then get its date. Since you already have the number of
this month and the year:

var monthdates = new Date(thisyear,(thismonth + 1),0).getDate();
And you''re done.

But rather than mess with that, here''s a routine that will do what you
want, it isn''t optimal for speed and needs the ''fromDate'' to be earlier
than the ''toDate''. Daylight saving should not be an issue and if your
system date is wrong... well, I can''t fix that from here. :-)

It could also be more concise, but it should do the job.
// Pass a date string with format YYYY-MM-DD,
// e.g. ''1994-10-15''
function dateDifYMD(ds){
// Create from and to date objects - from must be earlier date
var toDate = new Date();
var dsA = ds.split(''-'');
var fromDate = new Date(+dsA[0], +dsA[1]-1, +dsA[2]);

// Get month counts for both dates
var toMonths = toDate.getFullYear()*12 + toDate.getMonth();
var fromMonths = fromDate.getFullYear()*12 + fromDate.getMonth();

// Get difference in months
var months = toMonths - fromMonths;

// Subtract months from toDate months, fix if goes too far
toDate.setMonth(toDate.getMonth() - months);
if (toDate < fromDate) {
toDate.setMonth(toDate.getMonth()+1);
--months;
}
// Calc whole years and months
var years = Math.floor(months/12);
months = months%12; // or months = months - years*12;

// Subtract days one by one until gone too far
var days=0;
while (toDate > fromDate) {
toDate.setDate(toDate.getDate()-1);
++days;
}
// Adjust for going too far
--days;

// Format return string
var rString = years + ''year'' + ((years != 1)?''s'':'''') + '', ''
+ months + ''month'' + ((months != 1)?''s'':'''') + '', ''
+ days + ''day'' + ((days != 1)?''s'':'''');

return rString;
}

alert(dateDifYMD(''1994-10-15''));
[...]
--
Rob



no ** @ of .your.biz.nes 写道:
我设法得到以下脚本*几乎*工作。但是,它仍然有计算月数的问题。我试图计算的日期是1994年10月15日。使用正确的thatmonth
(10)它显示0.用9表示显示2,而不是1表示
正确。
I have managed to get the below script *almost* working. However, it
still has a problem calculating the number of months. The date I am
trying to calculate from is Oct 15, 1994. With the correct "thatmonth"
(10) it displays 0. With 9 it displays 2, instead of 1 which would be
correct.




除了RobG为您提供的帮助外,我还想告诉您

我试着写一个脚本我也觉得我有义务在我的初步(虽然可以解释)后做出反应。


但几分钟后我意识到这个任务*没有确切的

数学解*。计算两个日期之间

毫秒的数量是一个琐事。因此,计算

的秒数,分钟数,小时数和天数是一个琐事。


但你不能说在哪里正好是X年,Y个月和Z天

从那时起。


X是几年?标准,飞跃,3标准+ 1飞跃?

Y月份是多少?包括30天,29天,30 + 31对?


所以唯一的*可计算*答案可以是:Z天以来在哪里

然后。


PS除非我遗漏了一些东西,并且有一些共同的国际协议。



Besides the great help RobG provided to you, I wanted to tell you that
I tried to write a script for you too as I felt obligated after my
initial (though explicable) reaction.

But few minutes later I realized that this task *doesn''t have an exact
mathematical solution*. It is a trivia to calculate the amount of
milliseconds between two dates. So it''s a trivia to calculate the
amount of seconds, minutes, hours and days.

But you cannot say "where are exactly X years, Y months and Z days
since then".

X of what years? Standard, leap, 3 standard + 1 leap ?
Y of what month? Consisting of 30 days, 29 days, 30+31 pairs?

So the only *calculatable* answer can be: "where are Z days since
then".

P.S. Unless I''m missing something and there are some common
international agreements about this.


这篇关于计数脚本有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆