-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>
11
  • 1
    This link will help: stackoverflow.com/questions/1297231/… Commented Dec 19, 2019 at 5: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? Commented Dec 19, 2019 at 5:36
  • 3
    Does this answer your question? Convert string[] to int[] in one line of code using LINQ Commented Dec 19, 2019 at 5:47
  • 1
    what is 0700? is it 7Hr 0Min? Commented Dec 19, 2019 at 5:49
  • 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]) Commented Dec 19, 2019 at 6:08

1 Answer 1

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
{
    //  ............
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.