Converts datetime stored in the object to short date string. - CSharp System

CSharp examples for System:DateTime Convert

Description

Converts datetime stored in the object to short date string.

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;// ww  w  . j av  a2  s. co m

public class Main{
        //-------------------------------------------------------------------------
    /// <summary>
    /// Converts datetime stored in the object to short date string.
    /// </summary>
    static public string ToShortDateString(object o)
    {
      DateTime date;
      if (Parse(o, out date))
      {
        return date.ToShortDateString();
      }
      return "";
    }
        //-------------------------------------------------------------------------
    /// <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