472,373 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,373 software developers and data experts.

Create method for 24 hour time format

Important Note: I am not allowed to Use TimeSpan

I am trying to create C# method with two int arguments

Expand|Select|Wrap|Line Numbers
  1. public void Time(int hours, int minutes)
which satisfies all the following Test Cases:

Time(11, 7), returns: "11:07"
Time(24, 0), returns: "00:00"
Time(26, 0), returns: "02:00"
Time(0, 160), returns "02:40" (since 160 minutes = 2 hours and 40 minutes)
Time(-1, 0), returns: "23:00" (negative number - counterclockwise)
Time(1, -40), returns: "00:20"
Time(-25, -160), returns: "20:20"
Dec 30 '21 #1
2 9905
272 256MB
Here is an example that does not use any time-related functions.
Expand|Select|Wrap|Line Numbers
  1.             int h_div, h_rm, m_div, m_rm, i = 0;
  2.             while(minutes < 0){
  3.                 minutes = minutes + 60;
  4.                 i++;
  5.             }
  6.             hours = hours - i;
  7.             while(hours < 0) hours = hours + 24;
  8.             m_div = Math.DivRem(minutes, 60, out m_rm);
  9.             h_div = Math.DivRem(hours + m_div, 24, out h_rm);
  10.             Console.WriteLine(String.Format("{0:D2}:{1:D2}", h_rm, m_rm));
Jan 5 '22 #2
202 Expert 128KB
You can use DateTime and a one-liner:

Expand|Select|Wrap|Line Numbers
  1. int hours = -25;
  2. int minutes = -160;
  4. string textTime = DateTime.Today.AddHours(hours).AddMinutes(minutes).ToString("HH:mm");
  6. Console.WriteLine(textTime);
Jan 5 '22 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Lauren Wilson | last post by:
Is it possible to create a data input mask for a text box bound to a date/time field so that it will display and guide entry of the time values in 24 hour (military) time format?
by: Robert Misiak | last post by:
Hello- I live in the US, however I'm attempting to make a product of mine more international-friendly. There are a number of instances where a calendar function of my program displays various...
by: Just D | last post by:
Did anybody see this bug? I tried to reproduce that using 12-hour and 24-hour system settings. Anyway it still causes a problem in VS2003. When I convert DateTime to a string using default...
by: PinkBishop | last post by:
Hello, I have a simple calendar scipt that ask for time of event input. While I like the time feature I do not like the fact that it displays with seconds included. Ex. Displays HR:MIN:SEC...
by: Dogmar Hoffman | last post by:
Hello, I am trying to use tostring to convert to a 24 hour time format, but am getting the error "No Overload for Method 'ToString'takes '1' arguments". The following is what I have and it...
by: Julia | last post by:
Hi I have changed my regional options on my computer to English (United States) (I used to have Swedish). I would print the value of the current time in a textbox on my asp.net page. I use this...
by: kalisha | last post by:
I uploaded data in to postgresql using pdadminIII ,In one the fields called 'time' i want to store time as 24 hour format .Is there a way where i can convert time in to 24 hour format and store in...
by: kalisha | last post by:
how can i convert time in to a 24 hour time format using java?
by: tatata9999 | last post by:
Hi, What time zones tend to use 24 hours time format? Googling hasn't been able to answer the question. Thank you.
by: brendanmcdonagh | last post by:
I have these values in an array after using split $alltogether = $day . "," . $month . "," . $hour . "," . $minutes . ","; $arr=split(",",$alltogether); // splitting the array $d=$arr; //...
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.