#include<string>#include<vector>#include<cstdlib>usingnamespace std;
classSolution {
public:int days(string date) {
int y = stoi(date.substr(0,4)), m = stoi(date.substr(5,2)), d = stoi(date.substr(8,2));
int days = d;
staticint mdays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
for (int yy =1971; yy < y; ++yy)
days +=365+ ((yy%4==0&& yy%100!=0) || yy%400==0);
for (int mm =1; mm < m; ++mm) {
if (mm ==2&& ((y%4==0&& y%100!=0) || y%400==0)) days +=29;
else days += mdays[mm-1];
}
return days;
}
intdaysBetweenDates(string date1, string date2) {
return abs(days(date1)-days(date2));
}
};
classSolution {
publicintdaysBetweenDates(String date1, String date2) {
return Math.abs(days(date1)-days(date2));
}
intdays(String date) {
int y = Integer.parseInt(date.substring(0,4));
int m = Integer.parseInt(date.substring(5,7));
int d = Integer.parseInt(date.substring(8));
int days = d;
int[] mdays = {31,28,31,30,31,30,31,31,30,31,30,31};
for (int yy = 1971; yy < y; ++yy)
days += 365 + ((yy%4==0 && yy%100!=0) || yy%400==0 ? 1 : 0);
for (int mm = 1; mm < m; ++mm)
days += (mm==2 && ((y%4==0 && y%100!=0) || y%400==0)) ? 29 : mdays[mm-1];
return days;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
fundaysBetweenDates(date1: String, date2: String): Int {
fundays(date: String): Int {
val y = date.substring(0,4).toInt()
val m = date.substring(5,7).toInt()
val d = date.substring(8).toInt()
var days = d
val mdays = arrayOf(31,28,31,30,31,30,31,31,30,31,30,31)
for (yy in1971 until y)
days +=365 + if ((yy%4==0&& yy%100!=0) || yy%400==0) 1else0for (mm in1 until m)
days +=if (mm==2&& ((y%4==0&& y%100!=0) || y%400==0)) 29else mdays[mm-1]
return days
}
return kotlin.math.abs(days(date1)-days(date2))
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
defdaysBetweenDates(self, date1: str, date2: str) -> int:
defdays(date):
y, m, d = map(int, [date[:4], date[5:7], date[8:]])
mdays = [31,28,31,30,31,30,31,31,30,31,30,31]
total = d
for yy in range(1971, y):
total +=365+ ((yy%4==0and yy%100!=0) or yy%400==0)
for mm in range(1, m):
if mm ==2and ((y%4==0and y%100!=0) or y%400==0):
total +=29else:
total += mdays[mm-1]
return total
return abs(days(date1)-days(date2))
impl Solution {
pubfndays_between_dates(date1: String, date2: String) -> i32 {
fndays(date: &str) -> i32 {
let y = date[0..4].parse::<i32>().unwrap();
let m = date[5..7].parse::<i32>().unwrap();
let d = date[8..].parse::<i32>().unwrap();
let mdays = [31,28,31,30,31,30,31,31,30,31,30,31];
letmut total = d;
for yy in1971..y {
total +=365+if (yy%4==0&& yy%100!=0) || yy%400==0 { 1 } else { 0 };
}
for mm in1..m {
if mm ==2&& ((y%4==0&& y%100!=0) || y%400==0) {
total +=29;
} else {
total += mdays[(mm-1) asusize];
}
}
total
}
(days(&date1)-days(&date2)).abs()
}
}