By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,133 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,133 IT Pros & Developers. It's quick & easy.

Converting fraction to time

P: n/a
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

Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

Nov 17 '05 #2

P: n/a
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


Nov 17 '05 #3

P: n/a
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
>
>
>


Nov 17 '05 #4

P: n/a
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.

"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
>
>
>


Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.