我在写一个相当大的web应用asp.net/c#与MSSQL 2008 r2服务数据库.该程序需要转换date/time(ISO日期格式)字符串DateTime在使用它们的后来存储smalldatetime在sql.
当字符串转换为时datetimes,hour会神秘地添加到结果中.据我所知,在英国,我们需要夏令时(目前有效),但datetime.convert方法肯定能理解这一点吗?转换回字符串时,结果符合预期.
我写了一个小程序来说明问题(也包括我的理智的非ISO日期):
class Program
{
static void Main(string[] args)
{
//BB();
//Dist();
DateTime d1 = new DateTime();
DateTime d2 = new DateTime();
string d1s = "2010-09-13T09:30:01Z";
string d2s = "2010-09-13 09:30:01";
d1 = Convert.ToDateTime(d1s);
d2 = Convert.ToDateTime(d2s);
Console.WriteLine("d1s:{0} d1:{1} ", d1s, d1);
Console.WriteLine("d2s:{0} d2:{1} ", d2s, d2);
d1s = d1.ToString("u"); d2s = d2.ToString("u");
Console.WriteLine("\nd1: {0}", d1s);
Console.WriteLine("d2: {0}", d2s); …Run Code Online (Sandbox Code Playgroud)