Create TimeSpan From

In this chapter you will learn:

  1. Create a TimeSpan that represents a specified number of days
  2. Create a TimeSpan that represents a specified number of hours
  3. Returns a TimeSpan that represents a specified number of milliseconds
  4. Returns a TimeSpan that represents a specified number of minutes

From days

using System;/*  ja v  a2  s. c  om*/

class FromDaysDemo
{
    static void GenTimeSpanFromDays( double days )
    {
        TimeSpan interval = TimeSpan.FromDays( days );
        string   timeInterval = interval.ToString( );
        Console.WriteLine(timeInterval );
    } 

    static void Main( )
    {
        GenTimeSpanFromDays( 1 );
    } 
}

From hours

using System;//from  j  a  v a 2 s  .c o m

class FromHoursDemo
{
    static void GenTimeSpanFromHours( double hours )
    {
        TimeSpan    interval = TimeSpan.FromHours( hours );
        string      timeInterval = interval.ToString( );
        Console.WriteLine( timeInterval );
    } 

    static void Main( )
    {
        GenTimeSpanFromHours( 24 );
    } 
}

From milliseconds

using System;/*from jav  a  2s  .c o  m*/

class FromMillisecDemo
{
    static void GenTimeSpanFromMillisec( Double millisec )
    {
        TimeSpan    interval = TimeSpan.FromMilliseconds( millisec );
        string      timeInterval = interval.ToString( );
        Console.WriteLine(timeInterval );
    } 

    static void Main( )
    {
        GenTimeSpanFromMillisec( 3600000 );
    } 
}

From minutes

using System;/*from  j a  va 2  s  . c o m*/

class FromMinutesDemo
{
    static void GenTimeSpanFromMinutes( double minutes )
    {
        TimeSpan    interval = TimeSpan.FromMinutes( minutes );
        string      timeInterval = interval.ToString( );
        Console.WriteLine(timeInterval );
    } 

    static void Main( )
    {
        GenTimeSpanFromMinutes( 60 );
    } 
}

Next chapter...

What you will learn in the next chapter:

  1. What is TimeSpan
Home » C# Tutorial » Date, Time, TimeZone
Compare DateTimeOffset
DateTimeOffset creation
Date time Value from DateTimeOffset
Compare DateTime value
DateTime constant value
DateTime constructor
DateTime properties
Create TimeSpan From
TimeSpan
TimeSpan add and subtract
DateTime and TimeSpan
Compare TimeSpan
TimeSpan constant
TimeSpan Constructor
TimeSpan Duration
TimeSpan format
Negate a TimeSpan
TimeSpan operators
Parse string to TimeSpan
TimeSpan to string
TimeSpan total properties
TimeSpan propeties
Daylight saving
Time zone ID
UTC offset with TimeZone
TimeZone converting
TimeZone creation