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
"M. Uppal" <MU****@discussions.microsoft.com> wrote in message
news:12**********************************@microsof t.com...
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.
"Kevin Spencer" wrote:
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.
"M. Uppal" <MU****@discussions.microsoft.com> wrote in message
news:02**********************************@microsof t.com... > 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
>
>
>