[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1616
  • Last Modified:

C# class needed - last day of month

I am in a rush.  

Please provide working class that I can plug into my code:

I need the following

1)  Validation on Set StartDate value
2)  Validation on Set Offset value
3)  Finish the LastDayOfMonth function


Here is what I have done so far, please just plug in the missing pieces:

using System;

//Outer namespace will be called BuyersFund
namespace BuyersFund
{      //Inner namespace will be called Dates
      namespace Dates
      {
            /// <summary>
            /// Summary description for Class1.
            /// </summary>
            
            //Class name will be called DateFunctions
            public class DateFunctions
            {
                  //VBA function parameters StartDate and MonthOffset will become private data members of class DateFunctions of type string.
                  private String strStartDate;
                  private SmallInt siMonthOffset;

                  public DateFunctions()
                  {
                        //
                        // TODO: Add constructor logic here
                        //
                  }

                  public string DateString
                  {
                        set
                        {
                              strStartDate = value;
                        }
                        get
                        {
                              return strStartDate;
                        }
                  }

                  public SmallInt MonOffset
                  {
                        set
                        {
                              siMonthOffset = value;
                        }
                        get
                        {
                              return siMonthOffset;
                        }
                  }

                  public string LastDayOfMonth()
                  {
                        
                  }


            }
      }
}
0
Tom Knowlton
Asked:
Tom Knowlton
3 Solutions
 
msdixonCommented:
question: why is everything a string? make it a DateTime instead.

basically what you want to do is take the start date, set the day to 1 and the month to the current month plus one, then subtract 1 day. let the framework do all the calculations.

pretty simple really.
0
 
ptmcompCommented:
private DateTime StringToDateTime(string date)
{
    DateTime result;
    try
    {
        result = DateTime.Parse(date);  // if you need to support different locales there is an overloaded method which supports all date formats
    }
    catch(FormatException)
    {
        // handle case when the date format is not correct
    }
    return result;
}

public string LastDayOfMonth()
{
    DateTime dateTime;
    DateTime lastDayOfMonth;
    int offset;

    dateTime = StringToDate(strStartDate);
    lastDayOfMonth = new DateTime(dateTime.Year, dateTime.Month + 1, 1).AddDays(-1); // first day of next month -1 = last day of the month    
    return lastDayOfMonth.ToString("dddd"); // returns the name of the day (mm/dd/yyyy returns the date)
}
0
 
Tom KnowltonWeb developerAuthor Commented:
Did I mention I was in a rush?  :)


How is this?

using System;

//Outer namespace will be called BuyersFund
namespace BuyersFund
{      //Inner namespace will be called Dates
      namespace Dates
      {
            /// <summary>
            /// Summary description for Class1.
            /// </summary>
            
            //Class name will be called DateFunctions
            public class DateFunctions
            {
                  //VBA function parameters StartDate and MonthOffset will become private data members of class DateFunctions of type string.
                  private DateTime dtStartDate;
                  private Integer siMonthOffset;
                  private DateTime dtDate;

                  public DateFunctions()
                  {
                        //
                        // TODO: Add constructor logic here
                        //
                  }

                  public string DateString
                  {
                        set
                        {
                              //perform validation
                              if (value == "")
                              {
                                    dtStartdate = DateTime.Today;
                              }
                              else
                              {
                                    dtStartDate = value;
                              }
                        }
                        get
                        {
                              return dtStartDate;
                        }
                  }

                  public Int MonOffset
                  {
                        set
                        {
                              //datatype ensures will be numeric
                              if (IsNumeric(
                              siMonthOffset = value;
                        }
                        get
                        {
                              return siMonthOffset;
                        }
                  }

                  public string LastDayOfMonth()
                  {
                        dtDate = dtStartDate;
                        return dtDate.Day = 1;
                        return String(dtDate);
                  }


            }
      }
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
malharoneCommented:
what does SETOFFSET value do? just offset the date by XXX days?? (atleast that's what I'm assuming)

using System;

//Outer namespace will be called BuyersFund
namespace BuyersFund
{     //Inner namespace will be called Dates
   namespace Dates
   {
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
         
      //Class name will be called DateFunctions
      public class DateFunctions
      {
         //VBA function parameters StartDate and MonthOffset will become private data members of class DateFunctions of type string.
         private String strStartDate;
         private System.DateTime dt_;
         private int siMonthOffset;

         private void setDate(String dt)
         {
            try
            {
               dt_=DateTime.Parse(dt);
            }
            catch
            {
               dt_=DateTime.MinValue;
            }
         }

         public DateFunctions()
         {
            //
            // TODO: Add constructor logic here
            //
         }

         public string DateString
         {
            set
            {
               strStartDate = value;
               this.setDate(strStartDate);
            }
            get
            {
               return strStartDate;
            }
         }

         public int MonOffset
         {
            set
            {
               siMonthOffset = value;
               try
               {
                  int n =int.Parse(siMonthOffset);
                  dt_.AddDays(n);
               }
               catch
               {
               }
            }
            get
            {
               return siMonthOffset;
            }
         }

         public int LastDayOfMonth()
         {
                int m = dt_.Month;
            switch(m)
            {
               case 1:
               case 3:
               case 5:
               case 7:
               case 8:
               case 10:
               case 12:
                  return 31;
               case 4:
               case 6:
               case 9:
               case 11:
                  return 30;
               default:      //2
                  DateTime t;
                  try
                  {
                     t=new  DateTime(dt_.Year,dt_.Month,29);
                     return 29;
                  }
                  catch
                  {
                     return 28;
                  }
                       
               }
         }

      }
   }
}
0
 
ptmcompCommented:
   dateTime = StringToDate(strStartDate);
    DateTime lastDateOfMonth = new DateTime(dateTime.Year, dateTime.Month + 1, 1).AddDays(-1);
    lastDayOfMonth = lastDateOfMonth.Day;
0
 
Tom KnowltonWeb developerAuthor Commented:
What namespace is StringToDate in?
0
 
Tom KnowltonWeb developerAuthor Commented:
What do you all think about this?

~~~~~~~~~~~~~~~~~~~~~~~~

using System;

//Outer namespace will be called BuyersFund
namespace BuyersFund
{      //Inner namespace will be called Dates
      namespace Dates
      {
            /// <summary>
            /// Summary description for Class1.
            /// </summary>
            
            //Class name will be called DateFunctions
            public class DateFunctions
            {
                  //VBA function parameters StartDate and MonthOffset will become private data members of class DateFunctions of type string.
                  private DateTime dtStartDate;
                  private int iMonthOffset;                  
                  private DateTime dtDate;

                  public DateFunctions()
                  {
                        //
                        // TODO: Add constructor logic here
                        //
                  }

                  public DateTime DateString
                  {
                        set
                        {
                              //data type ensures will be a datetime value
                              dtStartDate = value;
                        }
                              
                        get
                        {
                              return dtStartDate;
                        }
                  }

                  public int MonOffset
                  {
                        set
                        {
                              //datatype ensures will be numeric
                              iMonthOffset = value;
                        }
                        get
                        {
                              return iMonthOffset;
                        }
                  }

                  public DateTime LastDayOfMonth()
                  {      
                        if (dtStartDate.Month == 12)
                        {
                              DateTime lastDateOfMonth = new DateTime(dtStartDate.Year + 1, 1, 1).AddDays(-1);
                              dtDate = lastDateOfMonth;
                        }
                        else
                        {
                              DateTime lastDateOfMonth = new DateTime(dtStartDate.Year, dtStartDate.Month + 1, 1).AddDays(-1);
                              dtDate = lastDateOfMonth;
                        }
                        
                        return dtDate;
                  }
            }
      }
}
0
 
ptmcompCommented:
             public DateTime LastDayOfMonth()
               {    
                    DateTime lastDateOfMonth = new DateTime(dtStartDate.Year, dtStartDate.Month, 1).AddMonths(1).AddDays(-1);
                    dtDate = lastDateOfMonth;
                   
                    return dtDate;
               }
0
 
Tom KnowltonWeb developerAuthor Commented:
Thanks everyone.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now