Reputation: 2877
I am currently pulling start times from an XML file and using a date checking method to filter and then display in a Listbox.Itemsource.
Currently the start times are in "yyyyMMddHHmmss zzz" format in my XML file and my DateChecking method is also the same format for comparison to work.
What I would like to do is to convert the format from "yyyyMMddHHmmss zzz" to Long format "Wednesday 19th June 12:00 PM and have this display in the listbox.
The Code #######################################################
namespace TV_Guide_Version2
{
public partial class TV2 : PhoneApplicationPage
{
public TV2()
{
InitializeComponent();
}
private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
{
WebClient c = new WebClient();
c.DownloadStringCompleted += new DownloadStringCompletedEventHandler(c_DownloadStringCompleted);
c.DownloadStringAsync(new Uri("http://www.Domain.com/XMLSrouce.xml?"));
}
bool MyDateCheckingMethod(string dateString)
{
DateTime now = DateTime.Now.Date.Add(DateTime.Now.TimeOfDay);
DateTime otherDate = DateTime.ParseExact(dateString, "yyyyMMddHHmmss K", null);
return (now.AddHours(-2) <= otherDate && otherDate <= now.AddHours(24));
}
void c_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
var r = XDocument.Parse(e.Result);
listBox1.ItemsSource = from tv in r.Root.Descendants("programme")
where tv.Attribute("channel").Value == "1201"
where MyDateCheckingMethod(tv.Attribute("start").Value)
let channelE1 = tv.Attribute("channel")
let startE1 = tv.Attribute("start")
let nameEl = tv.Element("title")
orderby tv.Attribute("start").Value ascending
let urlEl = tv.Element("desc")
select new TV1guide
{
DisplayName = nameEl == null ? null : nameEl.Value,
ChannelName = channelE1 == null ? null : channelE1.Value,
ChannelURL = urlEl == null ? null : urlEl.Value,
StartTime = startE1 == null ? (DateTime?)null : DateTime.ParseExact(startE1.Value, "yyyyMMddHHmmss zzz", DateTimeFormatInfo.CurrentInfo, DateTimeStyles.AssumeLocal),
};
}
public class TV1guide
{
public string DisplayName { get; set; }
public string ChannelURL { get; set; }
public string ImageSource { get; set; }
public DateTime? StartTime { get; set; }
public DateTime? EndTime { get; set; }
public string ChannelName { get; set; }
}
}
}
Upvotes: 0
Views: 1072
Reputation: 141618
This appears to do the trick:
var input = "20110901070000 +1100";
var dateTime = DateTimeOffset.ParseExact(input, "yyyyMMddHHmmss zzz", DateTimeFormatInfo.CurrentInfo);
var longFormat = dateTime.ToString("D");
Using DateTimeOffset
makes it easier when working with times in different timezones.
EDIT:
I think there are two problems we are trying to solve here, if I am not mistaken. Let's break it down:
I think the first bit is to make sure we get all of the information into a correct data type. Something like this:
var data =
from tv in r.Root.Descendants("programme")
where tv.Attribute("channel").Value == "1201"
let channelE1 = tv.Attribute("channel")
let startE1 = tv.Attribute("start")
let nameEl = tv.Element("title")
orderby tv.Attribute("start").Value ascending
let urlEl = tv.Element("desc")
let guide = new TV1guide
{
DisplayName = nameEl == null ? null : nameEl.Value,
ChannelName = channelE1 == null ? null : channelE1.Value,
ChannelURL = urlEl == null ? null : urlEl.Value,
StartTime = startE1 == null ? (DateTimeOffset?)null : ParseDate(startE1.Value),
}
where DateTimeOffset.Now.AddHours(-2) <= guide.StartTime && guide.StartTime <= DateTimeOffset.Now.AddDays(1)
select guide;
}
private static DateTimeOffset ParseDate(string value)
{
return DateTimeOffset.ParseExact(value, "yyyyMMddHHmmss zzz", DateTimeFormatInfo.CurrentInfo);
}
Now, if you wanted, you could introduce another property on TV1guide
that displays it the way you'd like:
public string StartTimeDisplay {get { return StartTime.HasValue ? StartTime.Value.ToString("D") : ""; }}
Or if you feel that it is too odd putting it there, you can place it whereever you'd like. THe point being now that it has been parsed into the TV1guide
, you can display it however you need to when the time comes.
Upvotes: 1