Specified object to convert the DateTimeOffset. - CSharp System

CSharp examples for System:Converter

Description

Specified object to convert the DateTimeOffset.

Demo Code


using System.Xml.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System.IO;//w w  w.  jav a  2s. c  o  m
using System.Globalization;
using System.Collections.Generic;
using System;

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

            if (value == null)
            { return null; }
            if (value is DateTimeOffset)
            { return (DateTimeOffset)value; }
            if (DateTimeOffset.TryParse(ToStringOrEmpty(value), formatProvider, dateTimeStyle, out x))
            { return x; }
            // System.Web.Script.Serialization.JavaScriptSerializer used in /Date DateTimeOffset000000000 from a string of the form.
            var m = rx.Match(ToStringOrEmpty(value));
            if (String.IsNullOrEmpty(m.Value) == false)
            { return new DateTimeOffset(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 DateTimeOffset? ToDateTimeOffset(Object value)
        {
            return ToDateTimeOffset(value, null, DateTimeStyles.AllowWhiteSpaces);
        }
        /// <summary>
        /// Specified object to convert the DateTimeOffset.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <param name="formatProvider"></param>
        /// <param name="dateTimeStyle"></param>
        /// <returns></returns>
        public static DateTimeOffset ToDateTimeOffset(Object value, DateTimeOffset defaultValue, IFormatProvider formatProvider, DateTimeStyles dateTimeStyle)
        {
            return ToDateTimeOffset(value, formatProvider, dateTimeStyle) ?? defaultValue;
        }
        /// <summary>
        /// Specified object to convert the DateTimeOffset.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static DateTimeOffset ToDateTimeOffset(Object value, DateTimeOffset defaultValue)
        {
            return ToDateTimeOffset(value) ?? defaultValue;
        }
}

Related Tutorials