Time Span To String - CSharp System

CSharp examples for System:TimeSpan

Description

Time Span To String

Demo Code

// Permission is hereby granted, free of charge, to any person obtaining a
using System.Text.RegularExpressions;
using System.Xml;
using System.Text;
using System.IO;//from   ww w . j a v a  2 s  . c  o  m
using System.Globalization;
using System.Collections.Generic;
using System.Collections;
using System;

public class Main{
        static public string TimeSpanToString (TimeSpan span)
		{
			StringBuilder sb = new StringBuilder ();

			if (span.Days > 0)
				sb.Append (span.Days + "d");

			if (span.TotalHours > 1.0)
				sb.Append (span.Hours + "h");

			if (span.TotalMinutes > 1.0)
				sb.Append (span.Minutes + "m");

			sb.Append (span.Seconds + "s");

			return sb.ToString ();
		}
}

Related Tutorials