-3

Im looking for a way to combine my integers together. I got 3 integers i want to make one from them.

All the integers are holding a time currency wich looks a little like this:

var date = DateTime.Now;
int timeHours = date.Hour;

I got the Hours, Minutes and Seconds and want to combine so they would look like this:

Hour : Minutes : Seconds

How can i combine the integers together to do that.

Note: I've looked on the internet but i could not get the information i was looking for.

This is what i looked at:

Combine two integers to create a unique number

How to combine 2 integers in order to get 1?

Community
  • 1
  • 1
The_Monster
  • 494
  • 2
  • 7
  • 28

6 Answers6

3

Combining these integers will generate a string, not an another integer. You can easily format your DateTime with ToString() method like;

var str = DateTime.Now.ToString("H':'m':'s"); // eg: 11:0:2

If you wanna get your hour, minute and second part with leading zeros for single digits, you can use HH:mm:ss format instead.

var str = DateTime.Now.ToString("HH':'mm':'ss"); // eg: 11:00:02
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • I believe you don't need quotes here - just `H:m:s` will do the job – Sergey Berezovskiy Jun 09 '15 at 08:13
  • @SergeyBerezovskiy It depends on what OP's `CurrentCulture` is. `:` character (which is [`TimeSeparator`](https://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.timeseparator%28v=vs.110%29.aspx)) has a special meaning as _replace me with current culture or supplied culture time separator_. That means if OP's current culture has a different time separator than `:`, output string will be different format. I know there are only 5 framework culture that has `.` as a time separator, _still_ there is a chance. – Soner Gönül Jun 09 '15 at 08:18
  • 1
    you are very globalize-able :) – Sergey Berezovskiy Jun 09 '15 at 09:20
  • 1
    @SergeyBerezovskiy Believe me, I wish I couldn't :) – Soner Gönül Jun 09 '15 at 09:59
2

DateTime.Now already contains all information you need, date and time. All you need to do is format this information

Melvin
  • 344
  • 1
  • 8
2

There're many ways to pack two (or many) integers into one based on their ranges, e.g.

  int i1 = 123;
  int i2 = 456;
  // two 32-bit integers into 64-bit one
  long result = (((long) i1) << 32) | i2; 

In your particular case

  int hours = 5;
  int minutes = 10;
  int seconds = 59;

  int combined = hours * 3600 + minutes * 60 + seconds;

reverse:

  int combined = 12345;

  int seconds = combined % 60;
  int minutes = (combined / 60) % 60;
  int hours = combined / 3600;
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • @GiorgiNakeuri In fairness, the question can not be solved exactly as asked - "I got 3 integers **i want to make one from them**." - This solves that, and is what I'd assumed was wanted too. – Octopoid Jun 09 '15 at 08:32
1

you can try the below code i think it's useful

       var date = DateTime.Now;
        var result = date.Hour + ":" + date.Minute + ":" + date.Second;
        Console.WriteLine(result);
Ashokreddy
  • 352
  • 1
  • 11
1

A simple way using the base 10 number system is to just

var number = hours * 10000 + minutes * 100 + seconds

this returns a number like 150936 for 15:09:36

To convert back:

seconds = number % 100
minutes = (number / 100) % 100
hours = number / 10000

Note that this is obviously not the most efficient approach, but simple

Lorentz Vedeler
  • 5,101
  • 2
  • 29
  • 40
0

You should cast to string with format function for example:

string result = string.Format("{0:00}:{1:00}:{2:00}", timeHours, timeMinutes, timeSeconds);

Just to be complete:

format HH:mm:ss:

string result = string.Format("{0:00}:{1:00}:{2:00}", timeHours, timeMinutes, timeSeconds);

string result = DateTime.Now.ToString("HH:mm:ss");

format H:m:s:

string result = string.Format("{0}:{1}:{2}", timeHours, timeMinutes, timeSeconds);

string result = DateTime.Now.ToString("H:m:s");
Giorgi Nakeuri
  • 35,155
  • 8
  • 47
  • 75