Specified object to convert the DateTime. - CSharp System

CSharp examples for System:Converter

Description

Specified object to convert the DateTime.

Demo Code


using System.Xml.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System.IO;//  ww  w .j ava  2  s  .  com
using System.Globalization;
using System.Collections.Generic;
using System;

public class Main{
        /// <summary>
        /// Specified object to convert the DateTime.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="formatProvider"></param>
        /// <param name="dateTimeStyle"></param>
        /// <returns></returns>
        public static DateTime? ToDateTime(Object value, IFormatProvider formatProvider, DateTimeStyles dateTimeStyle)
        {
            DateTime x;
            var rx = RegexList.AspNetAjaxJavaScriptDate;

            if (value == null)
            { return null; }
            if (value is DateTime)
            { return (DateTime)value; }
            if (DateTime.TryParse(ToStringOrEmpty(value), formatProvider, dateTimeStyle, out x))
            { return x; }
            // System.Web.Script.Serialization.JavaScriptSerializer??????????/Date000000000?????????????DateTime??????????????
            var m = rx.Match(ToStringOrEmpty(value));
            if (String.IsNullOrEmpty(m.Value) == false)
            { return new DateTime(1970, 1, 1).AddMilliseconds(Double.Parse(m.Groups["Ticks"].Value)).ToLocalTime(); }
            return null;
        }
        /// <summary>
        /// Specified object to convert the DateTime.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static DateTime? ToDateTime(Object value)
        {
            return ToDateTime(value, null, DateTimeStyles.AllowWhiteSpaces);
        }
        /// <summary>
        /// Specified object to convert the DateTime.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <param name="formatProvider"></param>
        /// <param name="dateTimeStyle"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(Object value, DateTime defaultValue, IFormatProvider formatProvider, DateTimeStyles dateTimeStyle)
        {
            return ToDateTime(value, formatProvider, dateTimeStyle) ?? defaultValue;
        }
        /// <summary>
        /// Specified object to convert the DateTime.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(Object value, DateTime defaultValue)
        {
            return ToDateTime(value) ?? defaultValue;
        }
}

Related Tutorials