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);I trying to communicate with Samsung X600 by using C# and AT Command set, but I can not find AT Comm...
By tuhack
Hello all, I'm writing a C# WinForms application that monitors a button of another application (non ...
By stevebrennan
Hi I need to use an ActiveX control in my Windows Service. It doesn’t need to have a form or show an...
By gombi
I want to create and supply a license for a control that I developed in C#. I have spent hours going...
By erichart
Hi,Does anybody know C# equivalent for Javascript escape function? Or Anybody have the C# implementa...
By lindashao, 1 Comments
I have read some posts regarding to unit testing in C#.net 2005 apps, many suggested NUnit. The MSTS...
By ssfftt, 1 Comments
Hi all, I'm creating an assembly which contains several images (as resources). I would like to know...
By dimitriclement, 2 Comments
Hiya,Does anyone know how to (if possible) turn on/off the email notification LED on a laptop, and i...
By leehorus, 4 Comments
HiI have this code in VBScript use the WMI method to create new windows user Account and add it to a...
By fraas, 4 Comments
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 ) ); // HHtimestring.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......
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 |