Parses the given hours string back to timespan. - CSharp System

CSharp examples for System:TimeSpan

Description

Parses the given hours string back to timespan.

Demo Code

/********************************************************************
 *  FulcrumWeb RAD Framework - Fulcrum of your business             *
 *  Copyright (c) 2002-2010 FulcrumWeb, ALL RIGHTS RESERVED         *
 *                                                                  *
 *  THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      *
 *  FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        *
 *  COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       *
 *  AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  *
 *  AND PERMISSION FROM FULCRUMWEB. CONSULT THE END USER LICENSE    *
 *  AGREEMENT FOR INFORMATION ON ADDITIONAL RESTRICTIONS.           *
 ********************************************************************/
using System.Globalization;
using System;//from w w w. j  a v  a  2s  . c o  m

public class Main{
        //-------------------------------------------------------------------------
    /// <summary>
    /// Parses the given hours string back to timespan.
    /// </summary>
    /// <returns>the time-span parsed, null if failed</returns>
    static public TimeSpan? ParseTimespanInHours(string str)
    {
      double hours;
      if (double.TryParse(str, out hours))
      {
        var hoursInt = Convert.ToInt32(Math.Floor(hours));
        var minutesInt = Convert.ToInt32((hours - Math.Floor(hours)) * 60);
        return new TimeSpan(hoursInt, minutesInt, 0);
      }
      return null;
    }
}

Related Tutorials