If given object is DateTime returns this date with lowest time, i.e. 01012008 00:00:00.000 for 01012008. - CSharp System

CSharp examples for System:DateTime Time

Description

If given object is DateTime returns this date with lowest time, i.e. 01012008 00:00:00.000 for 01012008.

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  w w . ja v a  2s  .  c o  m

public class Main{
        //-------------------------------------------------------------------------
    /// <summary>
    /// If given object is DateTime returns this date with lowest time,
    /// i.e. 01/01/2008 00:00:00.000 for 01/01/2008.
    /// </summary>
    /// <param name="dateObj">date object</param>
    /// <returns>date with lowest time</returns>
    static public object GetDateWithLowestTime(object dateObj)
    {
      DateTime date;
      if (Parse(dateObj, out date))
      {
        return GetDateWithLowestTime(date);
      }
      return dateObj;
    }
        //-------------------------------------------------------------------------
    /// <summary>
    /// Returns this date with lowest time, i.e. 01/01/2008 00:00:00.000 for 01/01/2008.
    /// </summary>
    /// <param name="date">date</param>
    /// <returns>date with lowest time</returns>
    static public DateTime GetDateWithLowestTime(DateTime date)
    {
      return new DateTime(date.Year, date.Month, date.Day, 0, 0, 0, 0);
    }
        //-------------------------------------------------------------------------
    /// <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