Home » Category » Microsoft Visual C#

Microsoft Visual C#: Time of day arithmetic

100| Tue, 25 Dec 2007 02:25:00 GMT| seanconnolly| Comments (5)

Hi.

I am working with a time string that I want to be able to add a value to. If I enter a time of day as hhmm (example 2000), I would like to add a time value as hhmm (0535 + 2000 would return 0135) and return this as a string.

Can someone help me with an easy way to do this please?

Thanks.

My current routine doesn't work, but here is what I have done in sourcecode. The only part that seems to fail is the adding of hours and minutes to the time. Is there a shorter or better way to do this?

public static string MinSearch(string MyTime, string MinConTime)

{

int MinMinutes;

int MinHours;

string timestring="";

DateTime StartTime = Convert.ToDateTime(MyTime.Substring(0,2)+":"+MyTime.Substring(2,2));

MinMinutes = Convert.ToInt32(MinConTime.Substring(2, 2));

MinHours = Convert.ToInt32(MinConTime.Substring(0, 2));

StartTime.AddMinutes(MinMinutes);

StartTime.AddHours(MinHours);

timestring = StartTime.ToString().Substring(11, 2) + StartTime.ToString().Substring(14, 2);

return (timestring);

Keywords & Tags: time, day, arithmetic, microsoft, visual c#, vc

URL: http://msdn.itags.org/visual-csharp/85470/
 
«« Prev - Next »» 5 helpful answers below.

I've fixed it! Spotted the obvious mistake

StartTime = StartTime.AddMinutes(MinMinutes) + StartTime.AddHours(MinHours);

seanconnolly | Fri, 31 Aug 2007 19:34:00 GMT |

Hah, I'm late.

Here is my 'corrected' code suggestion:

using System;

using System.Text;

namespace nsTest

{

class Program

{

public static string MinSearch( string MyTime, string MinConTime )

{

int MinMinutes;

int MinHours;

StringBuilder timestring = new StringBuilder( 6 );

DateTime StartTime = Convert.ToDateTime( MyTime.Substring( 0, 2 ) + ":" + MyTime.Substring( 2, 2 ) );

MinMinutes = Convert.ToInt32( MinConTime.Substring( 2, 2 ) );

MinHours = Convert.ToInt32( MinConTime.Substring( 0, 2 ) );

DateTime endTime = StartTime.AddMinutes( MinMinutes ).AddHours( MinHours );

// using SortableDateTimePattern 'yyyy-MM-ddTHH:mm:ss'

timestring.Append( endTime.ToString( "s" ).Substring( 11, 2 ) ); // HH

timestring.Append( endTime.ToString( "s" ).Substring( 14, 2 ) ); // mm

return ( timestring.ToString() );

}

static void Main()

{

Console.WriteLine( "MinSearch function 0535 + 2000 = {0}", MinSearch( "0535", "2000" ) );

Console.ReadKey();

} // end main

} // end class

} // end namespace

Is your code like this?

Why you need DateTime in your code? You can do more simple:

public static string MinSearch( string MyTime, string MinConTime )

{

int ret =( Convert.ToInt32( MinConTime)+Convert.ToInt32( MyTime)) % 2400;

return ret.ToString("0000");

}

Selection is in Your hand ]:()

peca55 | Fri, 31 Aug 2007 19:35:00 GMT |

Hi.

Thanks for your message. I don't need the date portion and I couldn't find something that just dealt with the time of day.

Also, your last little function for adding on time does not work correctly. I spotted a big flaw in the logic.

public static string MinSearch( string MyTime, string MinConTime )

{

int ret =( Convert.ToInt32( MinConTime)+Convert.ToInt32( MyTime)) % 2400;

return ret.ToString("0000");

}

If I used an input time of 2057 and added 0535 on, it returns 0192 which is not a valid time. It should have returned 0232 (add another 40 to the number if the right hand portion of the last two digits returned were >= 60).

I have only been dabbling in C# for the last 9-10 months, but with the pressure from my boss to get something working, I am progressing at full steam with trying to finish all logic operations rather than sit down and really get in deep with the language! Once the panic is over, I will sit down and properly learn the language and optimize it where I can!

Of the above, is there a way to easily detect the last two digits are higher than 60 and if so, to add the other 40?

Thanks.

Sean

seanconnolly | Fri, 31 Aug 2007 19:36:00 GMT |

Do not fight the DateTime class embrace it......

 public static string MinSearch( string MyTime, string MinConTime ) {
 DateTime StartTime = DateTime.ParseExact(MyTime,"HHmm", CultureInfo.CurrentCulture); TimeSpan MinTime = DateTime.ParseExact(MinConTime, "HHmm", CultureInfo.CurrentCulture) - DateTime.Today;
 DateTime endTime = StartTime + MinTime; return ( endTime.ToString("HHmm") );
 }

jamescurran | Fri, 31 Aug 2007 19:37:00 GMT |

Thank you James!

What an elegant way to do this!!! I didn't have any documentation about contolling the DateTime this way before. It's great!!!

seanconnolly | Fri, 31 Aug 2007 19:38:00 GMT |

Microsoft Visual C# Hot Answers

Microsoft Visual C# New questions

Microsoft Visual C# Related Categories