?
Solved

C# class needed - last day of month

Posted on 2003-11-26
9
Medium Priority
?
1,612 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:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 9

Assisted Solution

by:msdixon
msdixon earned 200 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:Tom 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Assisted Solution

by:malharone
malharone earned 200 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:Tom Knowlton
ID: 9828201
What namespace is StringToDate in?
0
 
LVL 5

Author Comment

by:Tom 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 1600 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:Tom Knowlton
ID: 9851145
Thanks everyone.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 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