473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 10866
SioSio
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
cactusdata
214 Expert 128KB
You can use DateTime and a one-liner:

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

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

Similar topics

1
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?
7
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...
3
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...
4
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...
6
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...
5
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...
1
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...
1
by: kalisha | last post by:
how can i convert time in to a 24 hour time format using java?
23
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.
3
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; //...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.