Specified object to convert the TimeSpan. - CSharp System

CSharp examples for System:Converter

Description

Specified object to convert the TimeSpan.

Demo Code


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

public class Main{
        /// <summary>
        /// Specified object to convert the TimeSpan.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TimeSpan? ToTimeSpan(Object value)
        {
            TimeSpan x;

            if (value == null)
            { return null; }
            if (value is TimeSpan)
            { return (TimeSpan)value; }
            if (TimeSpan.TryParse(ToStringOrEmpty(value), out x))
            { return x; }
            return null;
        }
        /// <summary>
        /// Specified object to convert the TimeSpan.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static TimeSpan ToTimeSpan(Object value, TimeSpan defaultValue)
        {
            return ToTimeSpan(value) ?? defaultValue;
        }
}

Related Tutorials