score:1

Accepted answer

First modify the class as per @abatischcev

public class PrayerTime
{
    public TimeSpan Fajr { get; set; }
    public TimeSpan Sunrise { get; set; }
    public TimeSpan Zuhr { get; set; }
}

Then modify the linq query select part as:

select new PrayerTime()
{
    Fajr = TimeSpan.Parse(c.Attribute("Fajr").Value),
    Sunrise = TimeSpan.Parse(c.Attribute("Sunrise").Value),
    Zuhr = TimeSpan.Parse(c.Attribute("Zuhr").Value)
};

then your check should be:

var obj = filteredData.First();
TimeSpan currentTime = myDay.TimeOfDay;
string result = String.Empty;
if (currentTime >= obj.Fajr && currentTime < obj.Sunrise)
{
    result = "Fajar";
}
else if (currentTime >= obj.Sunrise && currentTime < obj.Zuhr)
{
    result = "Zuhar";
}
textbox1.Text = result;

(By the way, Zuhr time should be between Zuhr and Asar :))

score:1

First, keep not string but TimeSpan object:

public TimeSpan Fajr { get; set; }
public TimeSpan Sunrise { get; set; }

To do this parse XML into DateTime:

TimeSpan ts = TimeSpan.Parse(c.Attribute("attr"));

So:

TimeSpan now = DateTime.Now.TimeOfDay; // time part only
var data = filteredData.First();

string result = null;
if (data.Fajr <= now && now < data.Sunrise); // notice operators greed
    result = data.Fajr;
else if (data.Sunrise <= now && now <= data.Zuhr)
    result = data.Zuhr;
myTextBox.Text = result;

score:0

The problem here is your code is "stringly typed". I would be better to use type that is design for time e.g. DateTime, but to quick fix it:

               // don't you need a third one here?
               select new PrayerTime()
               {
                   Fajr = c.Attribute("Fajr").Value,
                   Sunrise = c.Attribute("Sunrise").Value,
               };

Tu get current hour:

int currentHour = DateTime.Now.Hour;

Then is just simple comparison of two integers.

var data = filteredData.First();
int fajrHour = int.Parse(data.Fajr.Substring(0, 2), CultureInfo.InvariantCulture);
int sunriseHour = int.Parse(data.Sunrise.Substring(0, 2), CultureInfo.InvariantCulture);
int zuhrHour = int.Parse(data.Zuhr.Substring(0, 2), CultureInfo.InvariantCulture);

if(fajrHour <= currenHour && currenHour < sunriseHour)
{
    myTextBox.Text = data.Fajr; // or to show value fajrHour.ToString()
}
if(sunriseHour <= currenHour && currenHour  < zuhrHour)
{
    myTextBox.Text = data.Zuhr; //    zuhrHour.ToString()
}
// don't you need a third one here?

Related Articles