我目前正在從XML文件拉動開始時間,並使用日期檢查方法進行篩選,然後顯示在Listbox.Itemsource中。如何轉換DateTime?
目前,我的XML文件中的開始時間爲「yyyyMMddHHmmss zzz」格式,我的DateChecking方法也是用於比較工作的相同格式。
我想什麼做的是從「YYYYMMDDHHMMSS ZZZ」的格式轉換六月龍格式「19日下午12:00,並在列表框中此顯示。
守則##### ##################################################
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; }
}
}
}
你的問題到底是什麼?一切似乎都處於正常工作狀態。 – vcsjones
我的問題是「如何轉換爲長日期格式?」我認爲這很清楚。 – Rhys
向下箭頭?我認爲這有點苛刻。 – Rhys