If given object is DateTime returns this date with highest time - CSharp System

CSharp examples for System:DateTime Time

Description

If given object is DateTime returns this date with highest time

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 a  2s  .com

public class Main{
        //-------------------------------------------------------------------------
    /// <summary>
    /// If given object is DateTime returns this date with highest time,
    /// i.e. 01/01/2008 23:59:59.997 for 01/01/2008. 
    /// 997 is the highest number of milliseconds supported by SQL Server,
    /// 998 and 999 are rounded to 000.
    /// </summary>
    /// <param name="dateObj">date object</param>
    /// <param name="milliseconds">milliseconds for highest time</param>
    /// <returns>date with highest time</returns>
    static public object GetDateWithHighestTime(object dateObj, NxMaxMilliseconds milliseconds)
    {
      DateTime date;
      if (Parse(dateObj, out date))
      {
        return GetDateWithHighestTime(date, milliseconds);
      }
      return dateObj;
    }
        //-------------------------------------------------------------------------
    /// <summary>
    /// Returns this date with highest time, i.e. 01/01/2008 23:59:59.997 for 01/01/2008. 
    /// 997 is the highest number of milliseconds supported by SQL Server, 
    /// 998 and 999 are rounded to 000.
    /// </summary>
    /// <param name="date">date</param>
    /// <param name="milliseconds">milliseconds for highest time</param>
    /// <returns>date with highest time</returns>
    static public DateTime GetDateWithHighestTime(DateTime date, NxMaxMilliseconds milliseconds)
    {
      return new DateTime(date.Year, date.Month, date.Day, 23, 59, 59, Convert.ToInt32(milliseconds));
    }
        //-------------------------------------------------------------------------
    /// <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