Celcius_Bitz
Celcius_Bitz

Reputation: 75

Total days in last year

I have label1 label2 and button1 where i push button1, label1 text will changed based on last month total days (lets say '31' days) and label2 text will changed base on last years total days (lets say '365' days)

I only know how to coding last month total days which i use DateTime.DaysInMonth but there's no method for DateTime.DaysInYear isn't it

here's my code

private void button1_Click(object sender, EventArgs e)
{
    //Last Month
    string month = DateTime.Now.ToString("MM");
    int months = Int32.Parse(month);
    int previousmonths = months - 1;

    //Month in this Year
    string year = DateTime.Now.ToString("yyyy");
    int years = Int32.Parse(year);
    int daysmonth = DateTime.DaysInMonth(years, previousmonths);
    MessageBox.Show(daysmonth.ToString());

    //Last Year
    string year = DateTime.Now.ToString("yyyy");
    int years = Int32.Parse(year);
    int lastyears = years - 1;
    int daysyear = DateTime.DaysInYear(lastyears);
    MessageBox.Show(daysyear.ToString());
}

Upvotes: 2

Views: 2601

Answers (4)

Yaroslav Bres
Yaroslav Bres

Reputation: 1317

You can also use Calendar class. Example of using:

        var year = DateTime.UtcNow.AddYears(-1).Year;
        var daysInLastYear = Calendar.ReadOnly(new GregorianCalendar()).GetDaysInYear(year);

Upvotes: 0

evaon.yang
evaon.yang

Reputation: 11

or you can have this solution that help to count two day's count

class sample2
{
    internal static void Test()
    {
        var sampleTarget = new DateTime(2016, 4, 1);

        var dayInYearForTarget = DayInYear(sampleTarget);
        Console.WriteLine("Date: {0} => {1}", sampleTarget, dayInYearForTarget);
        Console.ReadKey();
    }

    private static double DayInYear(DateTime target)
    {
        var date = target.Date;
        var year = date.Year;

        return date.Subtract(new DateTime(year, 1, 1)).TotalDays + 1;
    }
}

Upvotes: 0

Yeldar Kurmangaliyev
Yeldar Kurmangaliyev

Reputation: 34189

As far as I remember, there are always 365 (366 days) in year (leap year).

You can simply use DateTime.IsLeapYear:

public static int DaysInYear(int year)
{
    return DateTime.IsLeapYear(year) ? 366 : 365;
}

However, looking at your code, I think that you overcomplicate it.
You don't need to format your DateTime as string and then parse it again.

Your code can be rewritten in a cleaner way:

// Days in previous Month
var monthAgo = DateTime.Now.AddMonths(-1);
int daysMonth = DateTime.DaysInMonth(monthAgo.Year, monthAgo.Month); 
MessageBox.Show(daysMonth.ToString());

// Days in Previous Year
int daysYear = DaysInYear(DateTime.Now.Year - 1); // see my function above
MessageBox.Show(daysYear.ToString());

Upvotes: 11

AlexDrenea
AlexDrenea

Reputation: 8039

There are some more elegant ways to get to last month and last year.

var lastMonth = DateTime.Now.AddMonths(-1).Month;
var lastYear = DateTime.Now.AddYears(-1).Year;

From there, getting the number of days in the year is simple like Yeldar pointed out:

var daysInLastYear = DateTime.IsLeapYear(yelastYear) ? 366 : 365;

Upvotes: 5

Related Questions