2011-08-27 97 views
0

我目前正在從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; } 

     } 




    } 
} 
+0

你的問題到底是什麼?一切似乎都處於正常工作狀態。 – vcsjones

+0

我的問題是「如何轉換爲長日期格式?」我認爲這很清楚。 – Rhys

+0

向下箭頭?我認爲這有點苛刻。 – Rhys

回答

1

這似乎這樣的伎倆:

var input = "20110901070000 +1100"; 
var dateTime = DateTimeOffset.ParseExact(input, "yyyyMMddHHmmss zzz", DateTimeFormatInfo.CurrentInfo); 
var longFormat = dateTime.ToString("D"); 

使用DateTimeOffset使得它更容易隨着時間的推移,當工作s在不同的時區。

編輯

我認爲有,我們正試圖在這裏解決,如果我沒有記錯的兩個問題。讓我們來分解它:

  1. 我們想要解析一個日期/時間以便乾淨地顯示。
  2. 我們希望按照該日期/時間進行過濾,以僅顯示某些記錄。

我認爲第一點是確保我們將所有信息都轉換爲正確的數據類型。事情是這樣的:現在

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); 
} 

,如果你願意,你可以在TV1guide引入另一個屬性顯示的方式,你希望:

public string StartTimeDisplay {get { return StartTime.HasValue ? StartTime.Value.ToString("D") : ""; }} 

或者,如果你覺得這是太奇把它放在那裏,你可以把它放在任何你想要的地方。現在,它已被解析爲TV1guide,你可以顯示它,但是當你需要的時候。

+0

謝謝你。我仍然不知道如何將它工作到我當前的日期比較代碼 – Rhys

+0

@Rhys:「檢查它」的目的是什麼?爲了確保它在一定範圍內?我會更新更多的細節。 – vcsjones

+0

檢查以基於當前時間顯示列表。並且消耗+小時。因此,它會根據當前的開始時間查找結果,並顯示前面2小時和12小時前的列表。 – Rhys