【发布时间】:2011-07-10 07:31:04
【问题描述】:
我正在研究将标准时间 + DST(“20110710115500 +1200”)转换为实际时间的项目。
它位于 XML 文件中,我可以提取和显示数据,但我希望将其转换为可读。
例如,“20110710115500 +1200”到 2011 年 10 月 7 日 11:55:00
我正在使用 Visual Studio 和 Silver Light,并将其用于 Windows Phone 应用程序。
我一直在阅读有关 TimeZoneInfo.ConvertTimeToUtc 方法(DateTime、TimeZoneInfo)的信息,但我似乎无法让它发挥作用,我希望有人能指出我正确的方向。
谢谢
我的代码..... StartTime 和 EndTime 以及我需要更改的日期。
编辑:我已经用您的更改更新了代码,但是当我尝试在模拟器上运行时它给了我一个错误。
错误:
“将字符串转换为日期时间时,先解析字符串取日期,然后再将每个变量放入日期时间对象”
注意:关于 C# 学习的权利,我目前正在阅读 C# 书籍。再次感谢您对此提供的帮助。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Xml;
using System.Xml.Linq;
namespace tvGuide
{
public partial class TV2 : PhoneApplicationPage
{
public TV2()
{
InitializeComponent();
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
WebClient c = new WebClient();
c.DownloadStringCompleted += new DownloadStringCompletedEventHandler(c_DownloadStringCompleted);
c.DownloadStringAsync(new Uri("http://www.designized.com/tv/freeview.xml?"));
}
void c_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
var r = XDocument.Parse(e.Result);
listBox2.ItemsSource = from tv in r.Root.Descendants("programme")
let channelE1 = tv.Attribute("channel")
let nameEl = tv.Element("title")
let urlEl = tv.Element("desc")
let startE1 = tv.Attribute("start")
let endE1 = tv.Attribute("stop")
//let iconEl = tv.Element("icon")
select new TV2guide
{
DisplayName = nameEl == null ? null : nameEl.Value,
ChannelName = channelE1 == null ? null : channelE1.Value,
ChannelURL = urlEl == null ? null : urlEl.Value,
StartTime = startE1 == null ? (DateTime?)null : DateTime.Parse(startE1.Value),
EndTime = endE1 == null ? (DateTime?)null : DateTime.Parse(endE1.Value),
//ImageSource = iconEl == null ? null : iconEl.Attribute("src").Value,
};
}
private void button3_Click_1(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
private void button4_Click_1(object sender, RoutedEventArgs e)
{
NavigationService.GoBack();
}
}
public class TV2guide
{
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; }
}
}
【问题讨论】:
-
由于您选择了一个时差为 12 小时的示例,因此不清楚是否要考虑偏移量。请举一个不同时区偏移的例子。 (顺便说一句,12 小时不是 DST……它是与 UTC 的完全偏移量。)
标签: xml silverlight linq windows-phone-7