To the time span string. - CSharp System

CSharp examples for System:TimeSpan

Description

To the time span string.

Demo Code


using System.Web;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Extensions;
using System.Web.Mvc;
using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System;// w  ww.  j  a v  a2s  .c  om

public class Main{
        /// <summary>
        /// To the time span string.
        /// </summary>
        /// <param name="ts">The ts.</param>
        /// <returns></returns>
        public static string ToTimeSpanString(this TimeSpan? ts)
        {
            if (ts.HasValue)
            {
                string hours = (ts.Value.TotalHours.ToString().IndexOf(".") != -1 ? ts.Value.TotalHours.ToString().Substring(0, ts.Value.TotalHours.ToString().IndexOf(".")) : ts.Value.TotalHours.ToString());
                return hours + ":" + (ts.Value.Minutes < 10 ? "0" + ts.Value.Minutes.ToString() : ts.Value.Minutes.ToString());
            }

            return "0";
        }
        /// <summary>
        /// To the time span string.
        /// </summary>
        /// <param name="ts">The ts.</param>
        /// <returns></returns>
        public static string ToTimeSpanString(this TimeSpan ts)
        {
            string hours = (ts.TotalHours.ToString().IndexOf(".") != -1 ? ts.TotalHours.ToString().Substring(0, ts.TotalHours.ToString().IndexOf(".")) : ts.TotalHours.ToString());
            hours = hours.Length == 1 ? "0" + hours : hours;
            return hours + ":" + (ts.Minutes < 10 ? "0" + ts.Minutes.ToString() : ts.Minutes.ToString());
        }
}

Related Tutorials