Create TimeSpan From
In this chapter you will learn:
- Create a TimeSpan that represents a specified number of days
- Create a TimeSpan that represents a specified number of hours
- Returns a TimeSpan that represents a specified number of milliseconds
- 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:
Home » C# Tutorial » Date, Time, TimeZone