473,480 Members | 4,827 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Converting fraction to time

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
4 11181
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1796
by: Krista Bailie | last post by:
I'm sure this is so easy that it will hurt for anyone to read it, but I really need some direction. I'm trying to create a color chart (RGB) that shows steps between two different colors as...
25
6623
by: TK | last post by:
I'm used to programming in c or c++ in which my problem is simple. I want to be able to enter a value on a page (like 3.2), and then read it as a 32-bit float and break it into it's individual...
2
15818
by: Benjamin Rutt | last post by:
Does anyone have C code laying around to do this? I have to read in some binary data files that contains some 4-byte IBM/370 floating point values. I would like a function to convert 4-byte...
5
2368
by: Valerie Hough | last post by:
Is there a C# class that will help convert strings that include fractions ( "2 1/2" e.g.) to doubles ? Convert.ToDouble() throws an invalid input format exception. Obviously I can write code to...
1
2186
by: d0ugg | last post by:
Hi, I'm did a fraction program for one of my programming classes and it did compile, however when I'm running the program it crashes for some reason that I do not know. // fraction.cpp ...
2
2980
by: d0ugg | last post by:
Hi, I'm doing a FRACTION program for one of my Programming classes and I'm getting some errors that I can't figure it out. Here is the Assignment: 1. Convert the fraction structure into a...
18
13008
by: Dirk Hagemann | last post by:
Hello, From a zone-file of a Microsoft Active Directory integrated DNS server I get the date/time of the dynamic update entries in a format, which is as far as I know the hours since january 1st...
4
1858
by: Semajthewise | last post by:
Hi All For those of you that helped me with the questions on this... Thank you! I believe I have solved all the bugs in the code and wanted to post the final product. This is a set of code to solve...
10
5298
by: Jason | last post by:
I'm making a program that will convert decimal inputs (in this case, in inches) and output a fractional answer. At the moment, I'm only able to output the fractional answer in three parts: A whole...
0
7040
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6905
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7041
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6736
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5331
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4478
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
561
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.