如何将最初为日期的序列号转换为该日期最初的天数?
How do I convert a serial number which was initially a date to the number of days that the date initially had?
首先我有一个日期(格式:dd、mm、yyyy)例如:21、3、2012 并将其转换为序列号,在本例中为 40988。现在我想找到我的问题一种算法,returns 最初给定的天数,因此在示例中它将是 21。这是我用来将日期转换为序列号的代码:
//intYears, intMonths, intDays are parameter variables
var serial = 0
//Going from 1900 to intYears - 1 (excluding the last year)
var arrNormalYears = new Array()
for (let i = 1900; i < intYears; i++) {
arrNormalYears.push(i)
}
//Count the number of normal years excluding the
//parameter year (intYear)
//the function numberOfDaysYear checks for us if the
//year is a leap year or a normal year
var normalYears = 0
for (let j = 0; j < arrNormalYears.length; j++) {
if (numberOfDaysYear(arrNormalYears[j]) == 365) {
normalYears += 1
}
}
//multiply the count and add it to serial
serial += normalYears * 365
//Same process for leap years
var arrLeapYears = new Array()
for (let m = 1900; m < intYears; m++) {
arrLeapYears.push(m)
}
//Count the number of leap years
var leapYears = 0
for (let a = 0; a < arrLeapYears.length; a++) {
if (numberOfDaysYear(arrLeapYears[a]) == 366) {
leapYears += 1
}
}
serial += leapYears * 366
//Now including the parameter variable intYear
//Using the same process as above except this time
//its for the months
var arrMonths = new Array()
for (let k = 1; k < intMonths; k++) {
arrMonths.push(k)
}
//Here the function numberOfDaysMonth gives us the
//number of days for the specific month, it also
//checks if it's a leap year
//also excluding the last month
for (let x = 0; x < arrMonths.length; x++) {
if (numberOfDaysMonth(arrMonths[x]) == 31) {
serial += 31
}
else if (numberOfDaysMonth(arrMonths[x]) == 30) {
serial += 30
}
else if (numberOfDaysMonth(arrMonths[x]) == 28) {
serial += 28
}
else if (numberOfDaysMonth(arrMonths[x]) == 29) {
serial += 29
}
}
//Simply adding the days that are left
serial += intDays
return serial
}
现在,通过了解我用于将日期转换为序列号的算法,我无法以某种方式反转算法和 return 上面代码中解释的日期。
您应该使用 setDate 和 替换 天数而不是像其他人所说的那样添加它。 JS Date 构造函数允许大于您通常认为允许的数字并给出正确的日期。
let serial = 40988;
let dayOfMonth = new Date(1900, 0, serial).getDate();
console.log(dayOfMonth);
更新
如果您将序列设置为零,您将得到 1899 年 12 月 31 日 - 您已经成功地重新创建了 OADate!原来都没注意到
注意
JavaScript 处理您当地时区的日期。只要您坚持使用单一时区 (local/UTC/whatever),您应该没问题。
首先我有一个日期(格式:dd、mm、yyyy)例如:21、3、2012 并将其转换为序列号,在本例中为 40988。现在我想找到我的问题一种算法,returns 最初给定的天数,因此在示例中它将是 21。这是我用来将日期转换为序列号的代码:
//intYears, intMonths, intDays are parameter variables
var serial = 0
//Going from 1900 to intYears - 1 (excluding the last year)
var arrNormalYears = new Array()
for (let i = 1900; i < intYears; i++) {
arrNormalYears.push(i)
}
//Count the number of normal years excluding the
//parameter year (intYear)
//the function numberOfDaysYear checks for us if the
//year is a leap year or a normal year
var normalYears = 0
for (let j = 0; j < arrNormalYears.length; j++) {
if (numberOfDaysYear(arrNormalYears[j]) == 365) {
normalYears += 1
}
}
//multiply the count and add it to serial
serial += normalYears * 365
//Same process for leap years
var arrLeapYears = new Array()
for (let m = 1900; m < intYears; m++) {
arrLeapYears.push(m)
}
//Count the number of leap years
var leapYears = 0
for (let a = 0; a < arrLeapYears.length; a++) {
if (numberOfDaysYear(arrLeapYears[a]) == 366) {
leapYears += 1
}
}
serial += leapYears * 366
//Now including the parameter variable intYear
//Using the same process as above except this time
//its for the months
var arrMonths = new Array()
for (let k = 1; k < intMonths; k++) {
arrMonths.push(k)
}
//Here the function numberOfDaysMonth gives us the
//number of days for the specific month, it also
//checks if it's a leap year
//also excluding the last month
for (let x = 0; x < arrMonths.length; x++) {
if (numberOfDaysMonth(arrMonths[x]) == 31) {
serial += 31
}
else if (numberOfDaysMonth(arrMonths[x]) == 30) {
serial += 30
}
else if (numberOfDaysMonth(arrMonths[x]) == 28) {
serial += 28
}
else if (numberOfDaysMonth(arrMonths[x]) == 29) {
serial += 29
}
}
//Simply adding the days that are left
serial += intDays
return serial
}
现在,通过了解我用于将日期转换为序列号的算法,我无法以某种方式反转算法和 return 上面代码中解释的日期。
您应该使用 setDate 和 替换 天数而不是像其他人所说的那样添加它。 JS Date 构造函数允许大于您通常认为允许的数字并给出正确的日期。
let serial = 40988;
let dayOfMonth = new Date(1900, 0, serial).getDate();
console.log(dayOfMonth);
更新
如果您将序列设置为零,您将得到 1899 年 12 月 31 日 - 您已经成功地重新创建了 OADate!原来都没注意到
注意
JavaScript 处理您当地时区的日期。只要您坚持使用单一时区 (local/UTC/whatever),您应该没问题。