【发布时间】:2016-07-22 00:38:19
【问题描述】:
注意:我无法使用日期功能
目标:转换从 1900 年 1 月 1 日到 2199 年 12 月 31 日的日期,序列号从 1 到 109573 不等。如果日期无效,则返回 0。
例子:
- dateSerialNumber(1, 1,1900) 返回 1
- dateSerialNumber(21, 3,2012) 返回 40988
- dateSerialNumber(31,12,2199) 返回 109573
- dateSerialNumber(31,12,1899) 返回 0
- dateSerialNumber(31,13,2000) 返回 0
- dateSerialNumber(29, 2,1991) 返回 0
更多示例: Examples of serial number by year
我还应该使用我已经编码的函数:
- dateValid(intDay, intMonth, intYear)
- numberDaysInYear(intYear)
- numberDaysInMonth(intMonth, intYear)
1)
function dateValid(intDay, intMonth, intYear) {
if ((intYear >= 1900 && intYear <= 2199) && (intMonth >= 1 && intMonth <= 12)
&& (intDay >= 1 && intDay <= numberDaysInMonth(intMonth, intYear))) {
return true;
}else return false;
}
2)
function numberDaysInYear(intYear) {
if(bissextile(intYear)) return '366'
else return '365'
}
3)
function numberDaysInMonth(intMonth, intYear) {
return new Date(intYear, intMonth, 0).getDate();
}
【问题讨论】:
-
序列号是多少?能改变吗?如果是基于毫秒的就更简单了。
-
您对挑战有什么问题?你到底有什么问题?到目前为止,您尝试过什么来解决它?
-
@Pred05 这似乎是自 1899 年 12 月 31 日以来的天数。如果该日期有效,则该日期的序列号为
0。 -
例如,1900 年 1 月 1 日是第 1 天,1900 年 1 月 31 日是第 31 天,1900 年 2 月 1 日是第 32 天,1900 年 2 月 2 日是第 33 天,等等。
-
不能使用 Date 对象?
标签: javascript numbers generator