Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# class needed - last day of month

Posted on 2003-11-26
9
Medium Priority
?
1,613 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

636 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