📜  查找给定日期在当前年份中的天数

📅  最后修改于: 2021-04-23 22:09:51             🧑  作者: Mango

给定一个字符串str ,它表示格式为YYYY-MM-DD的日期,任务是查找当前年份的天数。例如,1月1为一年,2的第1ND一月是一年的第二日,2月1是第32日一年等等。

例子:

方法:

  • 从给定的日期中提取年,月和日,并将它们存储在变量yearmonthday中
  • 创建一个数组days [] ,其中days [i]将存储第i月的天数。
  • 更新计数= days [0] + days [1] +…+ days [month – 1]以获取前几个月所有过去几天的计数。
  • 如果给定的年份是a年,则将此计数加1以便计数2月29
  • 最后,将day添加到计数中,该计数是当前月份中的天数,并打印最终计数

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
int days[] = { 31, 28, 31, 30, 31, 30,
               31, 31, 30, 31, 30, 31 };
  
// Function to return the day number
// of the year for the given date
int dayOfYear(string date)
{
    // Extract the year, month and the
    // day from the date string
    int year = stoi(date.substr(0, 4));
    int month = stoi(date.substr(5, 2));
    int day = stoi(date.substr(8));
  
    // If current year is a leap year and the date
    // given is after the 28th of February then
    // it must include the 29th February
    if (month > 2 && year % 4 == 0
        && (year % 100 != 0 || year % 400 == 0)) {
        ++day;
    }
  
    // Add the days in the previous months
    while (month-- > 0) {
        day = day + days[month - 1];
    }
    return day;
}
  
// Driver code
int main()
{
    string date = "2019-01-09";
    cout << dayOfYear(date);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
    static int days [] = { 31, 28, 31, 30, 31, 30,
                           31, 31, 30, 31, 30, 31 };
      
    // Function to return the day number
    // of the year for the given date
    static int dayOfYear(String date)
    {
        // Extract the year, month and the
        // day from the date string
        int year = Integer.parseInt(date.substring(0, 4));
          
        int month = Integer.parseInt(date.substring(5, 7));
          
        int day = Integer.parseInt(date.substring(8));
          
        // If current year is a leap year and the date
        // given is after the 28th of February then
        // it must include the 29th February
        if (month > 2 && year % 4 == 0 && 
           (year % 100 != 0 || year % 400 == 0))
        {
            ++day;
        }
      
        // Add the days in the previous months
        while (--month > 0)
        {
            day = day + days[month - 1];
        }
        return day;
    }
      
    // Driver code
    public static void main (String[] args)
    {
        String date = "2019-01-09";
        System.out.println(dayOfYear(date));
    }
}
  
// This code is contributed by ihritik


Python3
# Python3 implementation of the approach
days = [31, 28, 31, 30, 31, 30,
        31, 31, 30, 31, 30, 31];
  
# Function to return the day number
# of the year for the given date
def dayOfYear(date):
      
    # Extract the year, month and the
    # day from the date string
    year = (int)(date[0:4]);
    month = (int)(date[5:7]);
    day = (int)(date[8:]);
  
    # If current year is a leap year and the date
    # given is after the 28th of February then
    # it must include the 29th February
    if (month > 2 and year % 4 == 0 and 
       (year % 100 != 0 or year % 400 == 0)):
        day += 1;
  
    # Add the days in the previous months
    month -= 1;
    while (month > 0):
        day = day + days[month - 1];
        month -= 1;
    return day;
  
# Driver code
if __name__ == '__main__':
    date = "2019-01-09";
    print(dayOfYear(date));
  
# This code is contributed by Rajput-Ji


C#
// C# implementation of the approach
using System;
  
class GFG
{
    static int [] days = { 31, 28, 31, 30, 31, 30,
                           31, 31, 30, 31, 30, 31 };
      
    // Function to return the day number
    // of the year for the given date
    static int dayOfYear(string date)
    {
        // Extract the year, month and the
        // day from the date string
        int year = Int32.Parse(date.Substring(0, 4));
          
        int month = Int32.Parse(date.Substring(5, 2));
          
        int day = Int32.Parse(date.Substring(8));
          
        // If current year is a leap year and the date
        // given is after the 28th of February then
        // it must include the 29th February
        if (month > 2 && year % 4 == 0 && 
           (year % 100 != 0 || year % 400 == 0)) 
        {
            ++day;
        }
      
        // Add the days in the previous months
        while (--month > 0)
        {
            day = day + days[month - 1];
              
        }
        return day;
    }
      
    // Driver code
    public static void Main ()
    {
        String date = "2019-01-09";
        Console.WriteLine(dayOfYear(date));
    }
}
  
// This code is contributed by ihritik


输出:
9