ASP.NET Convert.ToDateTime 问题

本地正常, 服务器不正常

System.DateTime t = Convert.ToDateTime("2009\11\6 星期五 8:57:53");

代码

代码

本地浏览

本地

服务器浏览

图片说明

服务器Framework版本

服务器

本地版本

本地

这样肯定能解析,而且和系统设置无关了。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Q690391
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "2009\\11\\6 星期五 8:57:53";
            string sf = string.Format("{0:D4}-{1:D2}-{2:D2} {3:D2}:{4:D2}:{5:D2}", Regex.Matches(s, "\\d+").Cast<Match>().Select(x => (object)int.Parse(x.Value)).ToArray());
            DateTime dt = DateTime.ParseExact(sf, "yyyy-MM-dd hh:mm:ss", null);
            Console.WriteLine(dt);
        }
    }
}

图片说明

DateTime.ParseExact指定时间格式,不要依赖系统,依赖系统至少要使用常规的 xxxx-xx-xx HH:mm:ss格式


            DateTime t = DateTime.ParseExact(@"2009\11\6 星期五 8:57:53", @"yyyy\\M\\d dddd H:m:s", null);

这里改两个地方,第一个就是应用连接池改成经典的。
再一个系统获取时间的时候带中文的是因为你的本地电脑是Ghost版本的,所以你服务器上面获取时间是没有中文的,你如果要转,首先要把里面的中文去掉,这样才能保证服务器和本地是一样的
希望能够帮助到你