Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to be able to convert hours and minutes into this format hh:mm using C# DateTime library or TimeSpan (if possible).

For Example, if input for hours is 23 and input for minutes is 50, Output should be 23:50;
if input for hours is 25 and input for minutes is 60 output should be 02:00;
if input for hours is -1 and input for minutes is 0 output should be 23:00;

If it is possible, i would like this to be accomplished by modifying the code I provide below as little as possible.

Hours and Minutes must be read only properties as I have it below.

Thank You in Advance!

What I have tried:

public struct Time
{
    public Time(int hours, int minutes)
    {
        this.Hours = hours;
        this.Minutes = minutes;
    }

    public int Hours { get; }

    public int Minutes { get; }

    public override string ToString()
    {
        int initialTime = 0;
        return initialTime.(this.Hours).AddMinutes(this.Minutes).DateTime.ToString("t", CultureInfo.InvariantCulture);
    }

    public void Deconstruct(out int hours, out int minutes)
    {
        hours = this.Hours;
        minutes = this.Minutes;
    }
}
Posted
Updated 1-Jan-22 23:46pm
v3
Comments
[no name] 1-Jan-22 13:26pm    
You can use a DateTime or a TimeSpan; since you're mixing both concepts ("clock" and "timer/counter"), it's hard to suggest one over the other.
LuckyChloe 1-Jan-22 13:28pm    
TimeSpan also would be fine. Could you tell me what corrections should I make so that this code works? Thank you

You may be closer to a solution than you think you are: TimeSpan already supports "rollover," where a value less than, or more than, the "standard" ranges of month, day, hour, minute, values automatically increments/decrements, or decrements the next "higher" category. Try it:
// set break points, examine results
TimeSpan ts1 = new TimeSpan(22, 47, 90, 122);
TimeSpan ts2 = new TimeSpan(-12, -1, -122, 0, 0);

/* ts1 result:
{24.00:32:02}
    Days: 24
    Hours: 0
    Milliseconds: 0
    Minutes: 32
    Seconds: 2
    Ticks: 20755220000000
    TotalDays: 24.022245370370371
    TotalHours: 576.5338888888889
    TotalMilliseconds: 2075522000
    TotalMinutes: 34592.033333333333
    TotalSeconds: 2075522
*/
However, DateTime does not implement automatic rollover.
 
Share this answer
 
Comments
LuckyChloe 2-Jan-22 7:28am    
I solved the problem without using TimeSpan or DateTime
I used only if statements together with math operators
Thank you for your answer.
Could u pls share ur solution, still cant solve it
BillWoodruff 20-Jun-22 5:31am    
What have you tried ? And what solution are you after ?

Please be very specific.
I have exactly the same project to do. I tried to follow your solution, but it doesn't work when I input negative hours and positive minutes
BillWoodruff 21-Jun-22 2:53am    
Hi, I wanr to help you if I can !

Right now, I need more specific information on what your problem is.

I suggest you post a new question which includes your current code and an example of output that is not what you expect.

Please do not send the same comment twice.

cheers, Bill
With DateTime:
C#
string hhmm=new DateTime(2022, 1, 1, hours, minutes, 0).ToString("HH:mm");


Simpler, without DateTime:
C#
string hhmm=string.Format("{0:00}:{1:00}", hours, minutes);


:)
 
Share this answer
 
Comments
LuckyChloe 1-Jan-22 13:24pm    
Thank you for your answer. Can you tell me exactly where should I add that code block (first one)?
Luc Pattyn 1-Jan-22 13:29pm    
you can use it anywhere you want.
For instance, as a replacement of whatever your ToString() method currently contains (then don't forget to make it return hhmm)
LuckyChloe 1-Jan-22 13:34pm    
WIth that build fails. I get the following Error:

CS7036 There is no argument given that corresponds to the required formal parameter 'minutes' of 'Time.Time(int, int)' TimeStruct.Tests
LuckyChloe 1-Jan-22 13:36pm    
I replaced ToString() method code block with this one:

https://ibb.co/6BrC6BN
LuckyChloe 1-Jan-22 13:40pm    
Maybe I have not been explicit enough with the problem description. Those are the test cases that fail:

https://ibb.co/6WsqyCZ

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900