DateTime AddMinutes Format Issue

  • Thread starter Thread starter Fritz Switzer
  • Start date Start date
F

Fritz Switzer

I'd like to have a string assigned the value of a
DateTime.AddMinutes(amount) so that the string is formatted in "HH:MM"
format.

For example:

DateTime.Now.AddMinutes(30) returns "00:30"

DateTime.Now.AddMinutes(90) returns "1:30" or "01:30"

DateTime.Now.AdMinutes(-45)) returns "-00:45"

I've tried all kinds of Format specifiers and nothing is working, any help
is appreciated.

TIA,
 
Fritz,
Can you give a more complete example of what you are doing, and what you
expect?

As using the following:

Debug.WriteLine(DateTime.Now.AddMinutes(30), "30");
Debug.WriteLine(DateTime.Now.AddMinutes(90), "90");
Debug.WriteLine(DateTime.Now.AddMinutes(-45), "-45");

I get:

30: 7/19/2004 11:07:57 AM
90: 7/19/2004 12:07:57 PM
-45: 7/19/2004 9:52:57 AM

To limit the returned value to HH:MM I would use the ToString method with a
custom format, something like:

Debug.WriteLine(DateTime.Now.AddMinutes(30).ToString("hh:mm"), "30")
Debug.WriteLine(DateTime.Now.AddMinutes(90).ToString("hh:mm"), "90")
Debug.WriteLine(DateTime.Now.AddMinutes(-45).ToString("hh:mm"),
"-45")

30: 11:10
90: 12:10
-45: 09:55

For details on custom datetime formats see:

http://msdn.microsoft.com/library/d...s/cpguide/html/cpcondatetimeformatstrings.asp

For information on formatting in .NET in general see:
http://msdn.microsoft.com/library/d...y/en-us/cpguide/html/cpconformattingtypes.asp

Hope this helps
Jay
 
Sounds like you want to be using the timespan.

Ex:
TimeSpan ts = DateTime.Now.AddMinutes(30) - DateTime.Now;
string output = ts.ToString("hh:mm");
 
Adam,
As a short hand to " - DateTime.Now" you can use DateTime.TimeOfDay.
TimeSpan ts = DateTime.Now.AddMinutes(30).TimeOfDay;
string output = ts.ToString();

More importantly TimeSpan.ToString does not have any overloads, it returns
the value in a fixed format.

Hope this helps
Jay
 
Jay B. Harlow said:
Fritz,
Can you give a more complete example of what you are doing, and what you
expect?

Thanks Jay, here is more detail.

I'm using a random function that adds (- minutes) to the current time.
PickTime returns a negative number between 1-120. When the value is
returned the newTime is okay but just gives me total minutes , I just want
to format the result in hh:mm format. So if 95 minutes is returned I want
newTime to be "01:35" or if 46 is returned "00:46".

sting newTime =DateTime.Now.AddMinutes(-PickTime()).ToString();

So in your example:
Debug.WriteLine(DateTime.Now.AddMinutes(-45), "-45");

I'm looking to get 00:45 returned rather than.
-45: 7/19/2004 9:52:57 AM



It is being used as a "simulation", the value is attached to a timertick and
gets updated as time progresses. This seeds a value and the elapsed time is
updated with each tick, in this case, I'm updating a display every minute So
after 20 minutes the display using the above example of -45 I want the
value to be 01:05 (45+20 minutes or 1 hr 20 minutes)

hope that helps.

















As using the following:

Debug.WriteLine(DateTime.Now.AddMinutes(30), "30");
Debug.WriteLine(DateTime.Now.AddMinutes(90), "90");
Debug.WriteLine(DateTime.Now.AddMinutes(-45), "-45");

I get:

30: 7/19/2004 11:07:57 AM
90: 7/19/2004 12:07:57 PM
-45: 7/19/2004 9:52:57 AM

To limit the returned value to HH:MM I would use the ToString method with a
custom format, something like:

Debug.WriteLine(DateTime.Now.AddMinutes(30).ToString("hh:mm"), "30")
Debug.WriteLine(DateTime.Now.AddMinutes(90).ToString("hh:mm"),
"90")
Debug.WriteLine(DateTime.Now.AddMinutes(-45).ToString("hh:mm"),
"-45")

30: 11:10
90: 12:10
-45: 09:55

For details on custom datetime formats see:

http://msdn.microsoft.com/library/d...s/cpguide/html/cpcondatetimeformatstrings.asp

For information on formatting in .NET in general see:
http://msdn.microsoft.com/library/d...y/en-us/cpguide/html/cpconformattingtypes.asp

Hope this helps
Jay
 
Ahh... my mistake, I glanced at the MSDN docs on it too quickly (I saw the table listing the different parts, assumed it was
overloaded). Odd, though, that they did not overload it in the same manner as DateTime.

--
Adam Clauss
(e-mail address removed)


Jay B. Harlow said:
Adam,
As a short hand to " - DateTime.Now" you can use DateTime.TimeOfDay.
TimeSpan ts = DateTime.Now.AddMinutes(30).TimeOfDay;
string output = ts.ToString();

More importantly TimeSpan.ToString does not have any overloads, it returns
the value in a fixed format.

Hope this helps
Jay
 
Fritz,
I still don't follow you.

If your function returns 95, do you literally want "01:35" or do you want
"09:52" (which is Now - 95 minutes).

It sounds like you want "01:35", in which case you do not want to do
anything with DateTime.Now!

I would simply add minutes to DateTime.MinValue:

string newTime =
DateTime.MinValue.AddMinutes(PickTime()).ToString("hh:mm");

If you want "09:52" (Now - 95 minutes), then use DateTime.Now:

sting newTime =DateTime.Now.AddMinutes(-PickTime()).ToString("hh:mm");

If you have a specific time in mind, then use it:

DateTime whenever;
sting newTime =whenever.AddMinutes(-PickTime()).ToString("hh:mm");

In either case to the time formatted, you need to use
DateTime.ToString("hh:mm"), see my first reply for details on formatting.

Hope this helps
Jay

Fritz Switzer said:
Jay B. Harlow said:
Fritz,
Can you give a more complete example of what you are doing, and what you
expect?

Thanks Jay, here is more detail.

I'm using a random function that adds (- minutes) to the current time.
PickTime returns a negative number between 1-120. When the value is
returned the newTime is okay but just gives me total minutes , I just want
to format the result in hh:mm format. So if 95 minutes is returned I want
newTime to be "01:35" or if 46 is returned "00:46".

sting newTime =DateTime.Now.AddMinutes(-PickTime()).ToString();

So in your example:
Debug.WriteLine(DateTime.Now.AddMinutes(-45), "-45");

I'm looking to get 00:45 returned rather than.
-45: 7/19/2004 9:52:57 AM



It is being used as a "simulation", the value is attached to a timertick and
gets updated as time progresses. This seeds a value and the elapsed time is
updated with each tick, in this case, I'm updating a display every minute So
after 20 minutes the display using the above example of -45 I want the
value to be 01:05 (45+20 minutes or 1 hr 20 minutes)

hope that helps.

















As using the following:

Debug.WriteLine(DateTime.Now.AddMinutes(30), "30");
Debug.WriteLine(DateTime.Now.AddMinutes(90), "90");
Debug.WriteLine(DateTime.Now.AddMinutes(-45), "-45");

I get:

30: 7/19/2004 11:07:57 AM
90: 7/19/2004 12:07:57 PM
-45: 7/19/2004 9:52:57 AM

To limit the returned value to HH:MM I would use the ToString method
with
a
custom format, something like:

Debug.WriteLine(DateTime.Now.AddMinutes(30).ToString("hh:mm"), "30")
"90")
Debug.WriteLine(DateTime.Now.AddMinutes(-45).ToString("hh:mm"),
"-45")

30: 11:10
90: 12:10
-45: 09:55

For details on custom datetime formats see:
http://msdn.microsoft.com/library/d...s/cpguide/html/cpcondatetimeformatstrings.asp
For information on formatting in .NET in general see:
http://msdn.microsoft.com/library/d...y/en-us/cpguide/html/cpconformattingtypes.asp
 
Adam,
Odd, though, that they did not overload it in the same manner as DateTime.
I would expect it to be overloaded also, as sometimes I want to custom
format the Timespan, as this example just hours & minutes (no seconds).

Maybe we'll have to submit a suggestion for VS.NET 2005 at:

http://lab.msdn.microsoft.com/vs2005/

Jay

Adam Clauss said:
Ahh... my mistake, I glanced at the MSDN docs on it too quickly (I saw the
table listing the different parts, assumed it was
overloaded). Odd, though, that they did not overload it in the same manner as DateTime.
 
Thanks Jay/Adam,

If your function returns 95, do you literally want "01:35" or do you want
"09:52" (which is Now - 95 minutes).

I want "01:35"
It sounds like you want "01:35", in which case you do not want to do
anything with DateTime.Now!

Here are some additional details. I'm simulating a "doctors office", I want
to display the length in time (in "hh:mm") someone is waiting. I first
start by randomly assigning a time from -(1-120 minutes) so I use
DateTime.Now.AddMinutes(negative random amount). This then gives me length
of time, I'm just using minutes. So patient 1 = 35 minutes, patient 2= 65
minutes patient 3= 90 minutes . Now I need to display the time in a grid
control with the waiting time in "hh:mm" format, and just to make things
more complicated also sorted by longest wait time to shortest wait time.
So I can get 01:30, 01:05, and 00:35. After two minutes the display should
read 01:32, 01:07, 00:37. A timer is updating the display every minute.

hope that clears up things,

Thanks, appreciate the help.

Fritz
 
Fritz,
I have to ask, is this a home work assignment? Your questions & troubles
make this sounds suspiciously like a home work assignment. If it is I
strongly recommend you stop "cheating", if not, my apologies, proceed. Thank
you for understanding either way.

It really sounds like you are confusing:
- current time
- entry time
- elapsed time

As Now.AddMinutes(negative) will simulate entry time.

Now will simulate current time.

elapsed time = entry time - current time

It sounds like you want your grid to display elapsed time.

I would consider binding your Grid Control to a custom object, that has two
properties, as the custom object will allow you to easily update the elapsed
time value.

This custom object would need an Entry & an Elapsed property. Where you set
Entry to your Now.AddMinutes(negative), and Elapsed is Now.Subtract(Entry)

Note DateTime.Subtract will give you a TimeSpan, which you cannot custom
format, if you only want Elapsed to show hours & minutes I would add the
TimeSpan to DateTime.MinValue to convert it back to a DateTime...

Alternatively you could bind to a dataset, where you use a timer object and
& for loop to update the elapsed column of each row...

Hope this helps
Jay
 
Jay/Adam,

Not a "homework" assignment, real work. No apologies necessary and I
understand.

The requirement is for a grid control to show an elapsed time formatted in
"hh:mm", with the time in descending order. During development, I need to
simulate elapsed time, hence the seeding with the previous times for testing
only.

The confusion is finding the correct formating for "strings", "DateTime" and
"TimeSpan" and doing the calculations to show the elapsed time in "hh:mm".

The suggestions from both Jay and Adam have very been helpful.

Much appreciated.
 
Fritz,
Neither "String" nor "TimeSpan" support custom formatting. So your elapsed
time will need to be a DateTime. If you make it a DateTime, you can use
"hh:mm" for the format. Note it needs to be lower case, as "MM" is months...

Post if you really need help in how to get the grid to use your formatting.

Hope this helps
Jay
 
Jay,

Thanks for the help, I'll keep everything DateTime for now. That should
make the grid issue simpler.

Much appreciated.
 
Jay B. Harlow said:
Fritz,
Neither "String" nor "TimeSpan" support custom formatting. So your elapsed
time will need to be a DateTime. If you make it a DateTime, you can use
"hh:mm" for the format. Note it needs to be lower case, as "MM" is months...

Or - you can still use a TimeSpan -

TimeSpan ts = .....
string output = string.Format("%2d:%2d", ts.Hours, ts.Minutes);

That should give the format you want right?
 
Adam,
Not if your binding to a grid, unless you want to override the Format &
Parse events.

With all the trouble Fritz appears to be having I don't think its advisable
to push him toward a triathlon...

In other words yes you can do it that way. I just wonder if Fritz should do
it that way.

Just a thought
Jay
 
With all the trouble Fritz appears to be having I don't think its
advisable
to push him toward a triathlon...

I only particpate in events that last less than an hour. 00:59 :)

--
Fritz

Jay B. Harlow said:
Adam,
Not if your binding to a grid, unless you want to override the Format &
Parse events.

With all the trouble Fritz appears to be having I don't think its advisable
to push him toward a triathlon...

In other words yes you can do it that way. I just wonder if Fritz should do
it that way.

Just a thought
Jay


 
Back
Top