locked
(C#) Could someone help me fix and optimize my function? RRS feed

  • Question

  • Its rather simple, so it shouldn't be a problem to help. Just for a project I am working on and do not have many other sources of help.
    Thanks in advance :)!

    Pass day and month and year and 1 or 2 or 3 or 4 and it will return the conversion.
    Code:

    public static long getNum(long lngDayByVal, long lngMonthByVal, long lngYearByVal, long lngDWMY)
    {
        long lngCommonY = 0;
        long lngLeapY = 0;
        long lngDaysP = 0;
        for (long i = lngYearByVal + 1; i < DateTime.Now.Year; i++)
        {
            if (isLeapYear(i)) { lngLeapY++; } else { lngCommonY++; }
        }
        lngDaysP = (lngLeapY * 366) + (lngCommonY * 365);
        for (long i = 1; i < lngMonthByVal; i++)
        {
            if (i == 1 | i == 3 | i == 5 | i == 7 | i == 8 | i == 10 | i == 12)
            {
                lngDaysP += 31;
            }
            else if(i == 4 | i == 6 | i == 9 | i == 11)
            {
                lngDaysP += 30;
            }
            else if (i == 2)
            {
                if (isLeapYear(DateTime.Now.Year))
                {
                    lngDaysP += 29;
                }
                else
                {
                    lngDaysP += 28;
                }
            }
        }
        lngDaysP += lngDayByVal;
        if (lngDWMY == 1)
        {
            return lngDaysP;
        }
        else if (lngDWMY == 2)
        {
            return lngDaysP / 7;
        }
        else if (lngDWMY == 3)
        {
            return Int64.Parse(Math.Floor(lngDaysP / 30.4368499).ToString("N0"));
        }
        else
        {
            if ((lngMonthByVal <= DateTime.Now.Month) & (lngDayByVal <= DateTime.Now.Day))
            {
                return DateTime.Now.Year - lngYearByVal;
            }
            else
            {
                return (DateTime.Now.Year - lngYearByVal) - 1;
            }
        }
    }
    Wednesday, March 25, 2009 7:37 PM

Answers

  •  

    Hello Jowy,

     

    Thank you for your post!  I would suggest posting your question in the 'MSDN > Forums Home > Visual C# > Visual C# General
     'located here:  http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/threads/


    Have a great day!

    Thanks!

    SachinW Tier 2 Application Support Server and Tools Online Engineering Live Services Team
    • Proposed as answer by SachinW Thursday, March 26, 2009 12:31 AM
    • Marked as answer by SachinW Wednesday, May 20, 2009 9:00 AM
    Thursday, March 26, 2009 12:30 AM