给你一个按 YYYY-MM-DD 格式表示日期的字符串 date,请你计算并返回该日期是当年的第几天。
通常情况下,我们认为 1 月 1 日是每年的第 1 天,1 月 2 日是每年的第 2 天,依此类推。每个月的天数与现行公元纪年法(格里高利历)一致。
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/ordinal-number-of-date 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public:
int dayOfYear(string date) {
int monthDays[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int year = stoi(date.substr(0,4)),
month = stoi(date.substr(5,2)),
day = stoi(date.substr(8,2));
int sum = 0;
for(int i=1; i<month; i++)
sum += monthDays[i-1];
if(month > 2 &&
( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0) )
sum += 1;
sum += day;
return sum;
}
};