Solved

C# class needed - last day of month

Posted on 2003-11-26
9
1,559 Views
Last Modified: 2012-06-27
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
Comment
Question by:knowlton
9 Comments
 
LVL 9

Assisted Solution

by:msdixon
msdixon earned 50 total points
Comment Utility
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
 
LVL 10

Expert Comment

by:ptmcomp
Comment Utility
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
 
LVL 5

Author Comment

by:knowlton
Comment Utility
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
 
LVL 9

Assisted Solution

by:malharone
malharone earned 50 total points
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 10

Expert Comment

by:ptmcomp
Comment Utility
   dateTime = StringToDate(strStartDate);
    DateTime lastDateOfMonth = new DateTime(dateTime.Year, dateTime.Month + 1, 1).AddDays(-1);
    lastDayOfMonth = lastDateOfMonth.Day;
0
 
LVL 5

Author Comment

by:knowlton
Comment Utility
What namespace is StringToDate in?
0
 
LVL 5

Author Comment

by:knowlton
Comment Utility
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
 
LVL 10

Accepted Solution

by:
ptmcomp earned 400 total points
Comment Utility
             public DateTime LastDayOfMonth()
               {    
                    DateTime lastDateOfMonth = new DateTime(dtStartDate.Year, dtStartDate.Month, 1).AddMonths(1).AddDays(-1);
                    dtDate = lastDateOfMonth;
                   
                    return dtDate;
               }
0
 
LVL 5

Author Comment

by:knowlton
Comment Utility
Thanks everyone.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now