Converting fraction to time

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do i convert fraction to time in c#. I need to convert 0.25 to time.
Excel does this by using Selection.NumberFormat = "h:mm:ss;@" of the Cell
Format function.

thanks,

M. Uppal
 
First you have to identify what the fraction represents. 0.25 hours is a lot
longer than 0.25 seconds.

Second, you have to identify whether you are talking about a DateTime value
or a TimeSpan value.

Can you elaborate?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Complex things are made up of
Lots of simple things.
 
I am talking about DateTime. I have two part issue:

1) For Time Excel converts 0.25 to 6:00:00 AM using Selection.NumberFormat =
"h:mm:ss;@" on the cell containing 0.25.

2) For Date Excel converts 38626 to 10/1/05 using Format Cell function
Selection.NumberFormat = "mm/dd/yy;@" on the cell containg value 38626.

How do i accomplish these two converisons in C#? Hope this helps.

thanks for you help.
 
Hope this helps:

public class UlTime
{
#region Constants
public const long TICKS_IN_A_SECOND = 10000000;
public const int SECONDS_IN_A_DAY = 86400;
public const int SECONDS_IN_A_HOUR = 3600;
public const int SECONDS_IN_A_MINUTE = 60;
public const int MINUTES_IN_A_DAY = 1440;
public const int MINUTES_IN_A_HOUR = 60;
#endregion

#region TimeSpan to/from double
/// <summary>Converts a double into a timespan.</summary>
/// <param name="dValue">A double where the whole number part is days
and
/// the decimal portion represents some fraction of a day</param>
/// <returns>A Timespan.</returns>
public static TimeSpan DoubleToTimeSpan(double dValue)
{
int iDays = (int) dValue;
double dSeconds = Math.Floor( UlTime.SECONDS_IN_A_DAY * (dValue -
iDays) );
return new TimeSpan(iDays, 0, 0, (int) dSeconds, 0);
}
public static double TimeSpanToDouble( TimeSpan tsValue )
{
double dResult = tsValue.TotalSeconds / UlTime.SECONDS_IN_A_DAY;
return dResult;
}
#endregion

#region DateTime to/from double
/// <summary>Double to DateTime.</summary>
/// <param name="dValue">A double that represents time starting
/// on December 30, 1899 at midnight. The whole number part is days
and the
/// decimal portion represents some fraction of a day.</param>
/// <returns>A DateTime</returns>
public static DateTime DoubleToDateTime(double dValue)
{
DateTime dtResult = new DateTime(1899, 12, 30, 0, 0, 0, 0);
dtResult += DoubleToTimeSpan(dValue);
return dtResult;
}

/// <summary>Converts a datetime into a double.</summary>
/// <param name="dtValue">A C# datetime, which is value type that
represents
/// dates and times with values ranging from 12:00:00 midnight,
January 1, 0001
/// Anno Domini (Common Era) to 11:59:59 P.M., December 31, 9999 A.D.
(C.E.)</param>
/// <returns>A double that represents time starting on December 30,
1899
/// at midnight. The whole number part is days and the decimal
portion represents
/// some fraction of a day.</returns>
public static double DateTimeToDouble( DateTime dtValue )
{
TimeSpan tsTemp = dtValue - new DateTime(1899, 12, 30, 0, 0, 0, 0);
double dResult = tsTemp.TotalSeconds / UlTime.SECONDS_IN_A_DAY;
return( dResult );
}
#endregion
}

Dave
 
It sounds like Excel is working with the value as a fraction of a day. This
would be based upon a DateTime.MinValue of 1/1/0000.

In the .Net platform, there is no time, but only a DateTime. But to convert
..25 to 6:00 AM, you could simply use:

DateTime dt = DateTime.MinValue.AddDays(0.25D);

The same would go for converting the 38626 value:

DateTime dt = DateTime.MinValue.AddDays(38626D);

The method takes a double, so you could write a function to do this:

public static DateTime ConvertDays(double days)
{
return DateTime.MinValue.AddDays(days);
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Complex things are made up of
Lots of simple things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top