Converts DateTime to its equivalent string representation. - CSharp System

CSharp examples for System:DateTime Format

Description

Converts DateTime to its equivalent string representation.

Demo Code


using System.Globalization;
using System;//from ww  w.  j a  v a  2  s.c  o  m

public class Main{
        /// <summary>
        /// Converts a <see cref="Nullable"/> <see cref="DateTime"/> to its equivalent string representation. 
        /// </summary>
        /// <param name="dateTime">The <see cref="Nullable"/> <see cref="DateTime"/> to convert.</param>
        /// <returns><see langword="null"/> if A string representation <paramref name="dateTime"/>.</returns>
        public static string ToString(DateTime? dateTime)
        {
            if (dateTime.HasValue)
            {
                return ToString(dateTime.Value);
            }
            else
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// Converts a <see cref="DateTime"/> to its equivalent string representation. 
        /// </summary>
        /// <param name="dateTime">The <see cref="DateTime"/> to convert.</param>
        /// <returns>A string representation <paramref name="dateTime"/>.</returns>
        public static string ToString(DateTime dateTime)
        {
            return dateTime.ToString(DateTimeFormat);
        }
}

Related Tutorials