-1

can you please help me. i have a problem with my code. i have string array and i want to convert in int then apply an if else condition.

Military Time

string[] FlaggingTime = ((Hashtable)ConfigurationSettings.GetConfig("Time"))["FlaggingTime"].ToString().Trim().Split(new char[] { ',' });
string FlaggingTimeBuffer = ((Hashtable)ConfigurationSettings.GetConfig("Time"))["FlaggingTimeBuffer"].ToString();

if (Convert.ToInt32(DateTime.Now.ToString("HHmm")) > Convert.ToInt32(FlaggingTime) && Convert.ToInt32(DateTime.Now.ToString("HHmm")) < (Convert.ToInt32(FlaggingTime) + Convert.ToInt32(FlaggingTimeBuffer)) )
{
    //Do Something
}
else
{
    //Do Something
}            

Configurable File

<Time>
    <add key="FlaggingTime" value="0700,0900"/>
    <add key="FlaggingTimeBuffer" value="100"/>
</Time>
Hammad Sajid
  • 312
  • 1
  • 3
  • 14
iMemew
  • 59
  • 7
  • 1
    This link will help: https://stackoverflow.com/questions/1297231/convert-string-to-int-in-one-line-of-code-using-linq – Sahil Sharma Dec 19 '19 at 05:36
  • please clarify more. Edit your question. Whether `buffer` is in seconds? whether the time is in 12 hr clock or in 24 hr clock? – Sushant Yelpale Dec 19 '19 at 05:36
  • Performing time and period of times comparisons can be tricky. C# has some great ways to help with this. You should look at the `System.DateTime` and `System.TimeSpan` structs: https://learn.microsoft.com/en-us/dotnet/api/system.datetime?view=netframework-4.8 and https://learn.microsoft.com/en-us/dotnet/api/system.timespan?view=netframework-4.8 – STLDev Dec 19 '19 at 05:37
  • 3
    Does this answer your question? [Convert string\[\] to int\[\] in one line of code using LINQ](https://stackoverflow.com/questions/1297231/convert-string-to-int-in-one-line-of-code-using-linq) – Shree Harsha Dec 19 '19 at 05:47
  • 1
    what is `0700`? is it `7Hr 0Min`? – Sushant Yelpale Dec 19 '19 at 05:49
  • @SushantYelpale, its a military timing so they denote timing in 4 digit format `0700` means `7:00AM`, `2000` means `08:00 PM` – Prasad Telkikar Dec 19 '19 at 05:51
  • i use military time 0700 = 07:00am – iMemew Dec 19 '19 at 05:51
  • clarify a bit more, which flagging time you want to compare `0700` or `0900`? – Sushant Yelpale Dec 19 '19 at 05:54
  • i want to compare both. like if ```07:45am``` **true** when ```08:45am``` **false** and if the time is ```09:45am``` **true**. that's why i use array because of flexible time checking. the user can edit the **FlaggingTime** what they prefer. like the user edit the ```0900``` to ```1000``` or the user add a FlaggingTime ```0700,0900,1000``` . like that. Sorry for bad english and grammar. – iMemew Dec 19 '19 at 05:57
  • 1
    @iMemew, FlaggingTime is an array, so you might want to use a forloop and put your If condition with it and then compare. Ex Convert.ToInt32(DateTime.Now.ToString("HHmm")) >Convert.ToInt32(FlaggingTime[0]) – Clint Dec 19 '19 at 06:08
  • Instead of a bunch of `Convert.ToInt`s inside the condition, convert the various values into separate `int` variables, then check those in the condition. That makes reasoning and debugging easier. Plus as Clint said, you need to check every value in the array separately, so you need to loop over it – Hans Kesting Dec 19 '19 at 07:32

1 Answers1

0

Assuming FlaggingTimeBuffer is in seconds, Does this satisfy your need?

string[] time ={
    "0700",
    "0900"
};
string buffer = "100";

TimeSpan timeOfDay = DateTime.Now.TimeOfDay;
var FlaggingTimes = time.Select(e => TimeSpan.ParseExact(e, "hhmm", CultureInfo.InvariantCulture)).ToList();

if (timeOfDay > FlaggingTimes[0] && timeOfDay < FlaggingTimes[1].Add(new TimeSpan(0, Convert.ToInt16(buffer), 0)))
{
    //  ............
}
else
{
    //  ............
}
Sushant Yelpale
  • 860
  • 6
  • 19