String extension method which tries to convert the input string into DateTime representation. - CSharp System

CSharp examples for System:DateTime Convert

Description

String extension method which tries to convert the input string into DateTime representation.

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Security.Cryptography;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System;/*from   w  ww.j ava 2 s  . c  o m*/

public class Main{
        /// <summary>
        /// String extension method which tries to convert the input string into DateTime representation.
        /// </summary>
        /// <param name="input">The string on which the method is invoked.</param>
        /// <returns>Returns variable of type DateTime if the string can be parsed, null - otherwise.</returns>
        public static DateTime ToDateTime(this string input)
        {
            DateTime dateTimeValue;
            DateTime.TryParse(input, out dateTimeValue);
            return dateTimeValue;
        }
}

Related Tutorials