## Algorithm

Problem Name: 1154. Day of the Year

Given a string `date` representing a Gregorian calendar date formatted as `YYYY-MM-DD`, return the day number of the year.

Example 1:

```Input: date = "2019-01-09"
Output: 9
Explanation: Given date is the 9th day of the year in 2019.
```

Example 2:

```Input: date = "2019-02-10"
Output: 41
```

Constraints:

• `date.length == 10`
• `date[4] == date[7] == '-'`, and all other `date[i]`'s are digits
• `date` represents a calendar date between Jan 1st, 1900 and Dec 31th, 2019.

## Code Examples

### #1 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
List daysInMonth = Arrays.asList(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
public int dayOfYear(String date) {
int year = Integer.parseInt(date.split("-")[0]);
int month = Integer.parseInt(date.split("-")[1]);
int day = Integer.parseInt(date.split("-")[2]);

int numOfDays = getNumOfDays(month, isLeapYear(year));

return numOfDays + day;
}

private boolean isLeapYear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
return year % 400 == 0;
}
else {
return true;
}
}
return false;
}

private int getNumOfDays(int month, boolean isLeapYear) {
int numOfDays = 0;
for (int i = 1; i  <  month; i++) {
if (i == 2) {
numOfDays += isLeapYear ? daysInMonth.get(i - 1) + 1 : daysInMonth.get(i - 1);
}
else {
numOfDays += daysInMonth.get(i - 1);
}
}
return numOfDays;
}
}
``````
Copy The Code &

Input

cmd
date = "2019-01-09"

Output

cmd
9

### #2 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const dayOfYear = function(date) {
const [year, month, day] = date.split('-').map(s => +s),
months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
isLeapYear = !(year % 4) && month > 2 && (!!(year % 100) || !(year % 400))
return months.splice(0, month - 1).reduce((a, b) => a + b, day + +isLeapYear)
}
``````
Copy The Code &

Input

cmd
date = "2019-01-09"

Output

cmd
9

### #3 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
def dayOfYear(self, date: str) -> int:
cnt = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
y, m, d = map(int, date.split('-'))
days = sum(cnt[:m - 1]) + d
if m > 2:
if y % 400 == 0: days += 1
if y % 100 == 0: return days
if y % 4 == 0: days += 1
return days
``````
Copy The Code &

Input

cmd
date = "2019-02-10"

Output

cmd
41

### #4 Code Example with C# Programming

```Code - C# Programming```

``````
using System.Linq;

namespace LeetCode
{
public class _1154_DayOfTheYear
{
public int DayOfYear(string date)
{
var days = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

var split = date.Split('-').Select(s => int.Parse(s)).ToArray();
var year = split[0];
var count = 0;

for (int i = 0; i  <  12; i++)
{
if (split[1] == i + 1)
{
count += split[2];
break;
}
else
{
count += days[i];
}
}

if (split[1] > 2)
if ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0)) count++;

return count;
}
}
}
``````
Copy The Code &

Input

cmd
date = "2019-02-10"

Output

cmd
41