Solved

C# class needed - last day of month

Posted on 2003-11-26
9
1,599 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
ID: 9827818
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
ID: 9827867
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
ID: 9827870
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 9

Assisted Solution

by:malharone
malharone earned 50 total points
ID: 9828017
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
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9828159
   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
ID: 9828201
What namespace is StringToDate in?
0
 
LVL 5

Author Comment

by:knowlton
ID: 9828482
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
ID: 9830089
             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
ID: 9851145
Thanks everyone.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 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