If given object is DateTime, returns date of last day of the month. - CSharp System

CSharp examples for System:DateTime Day

Description

If given object is DateTime, returns date of last day of the month.

Demo Code

/********************************************************************
 *  FulcrumWeb RAD Framework - Fulcrum of your business             *
 *  Copyright (c) 2002-2010 FulcrumWeb, ALL RIGHTS RESERVED         *
 *                                                                  *
 *  THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      *
 *  FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        *
 *  COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       *
 *  AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  *
 *  AND PERMISSION FROM FULCRUMWEB. CONSULT THE END USER LICENSE    *
 *  AGREEMENT FOR INFORMATION ON ADDITIONAL RESTRICTIONS.           *
 ********************************************************************/
using System.Globalization;
using System;/*from  w ww  .  j a  v  a2 s.c o  m*/

public class Main{
        //-------------------------------------------------------------------------
    /// <summary>
    /// If given object is DateTime, returns date of last day of the month.
    /// </summary>
    /// <param name="dateObj">date object</param>
    /// <returns>date of last day of the month</returns>
    static public object GetLastMonthDay(object dateObj)
    {
      DateTime date;
      if (Parse(dateObj, out date))
      {
        return GetLastMonthDay(date);
      }
      return dateObj;
    }
        //-------------------------------------------------------------------------
    /// <summary>
    /// Returns date of last day of the month.
    /// </summary>
    /// <param name="date">date</param>
    /// <returns>date of last day of the month</returns>
    static public DateTime GetLastMonthDay(DateTime date)
    {
      return new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
    }
        //-------------------------------------------------------------------------
    /// <summary>
    /// Parses datetime value using standard format.
    /// </summary>
    /// <param name="o">value to parse</param>
    /// <returns>parsed datetime value</returns>
    static public DateTime Parse(object o, DateTime defaultValue)
    {
      DateTime d;
      if (Parse(o, out d))
      {
        return d;
      }
      else
      {
        return defaultValue;
      }
    }
        //--------------------------------------------------------------------------
    /// <summary>
    /// Parses datetime value using standard format.
    /// </summary>
    /// <param name="o">value to parse</param>
    /// <returns>parsed datetime value</returns>
    static public bool Parse(object o, out DateTime d)
    {
      d = DateTime.MinValue;
      if (CxUtils.IsEmpty(o))
      {
        return false;
      }
      if (o is DateTime)
      {
        d = (DateTime)o;
        return true;
      }
      try
      {
        d = Convert.ToDateTime(o);
        return true;
      }
      catch
      {
        return false;
      }
    }
}

Related Tutorials