某个程序使用php中的time()数据向mysql写入日期(如:1125114878 as int)
而后我将mysql库转换成sqlserver,发现日期字段类型为decimal,内容为:1125114878进过咨询,在php中可用如下方法还原:
date('Y-m-d h:i:s',time());那么在c#,或者在t-sql中是否有同样的方法可以对数据库中的日期内容进行还原!?

解决方案 »

  1.   

    在Sql server日期时间的类型为DateTime类型。在C#中只要对Sql Server中取出的DateTime时间强制转换一下就可以了。如:(DateTime)******    (*****)为数据库里取出来的对象。
      

  2.   

    decimal str=1124899200;
    DateTime t;
    t=(DateTime)str;
    this.Label1.Text=t.ToString();编译提示无法将decimal转换为datetime格式!
      

  3.   

    你看看是不是以100毫微秒表示的日期时间格式
    decimal str=1124899200;
    DateTime dt = new DateTime(str);
    检验一下对不对
      

  4.   

    long str=1056804717;
    DateTime t=new DateTime(str);            
    this.Label1.Text=t.ToString();返回时间不对,可能是unix的时间戳格式和windows的时间戳格式不同吧!?
      

  5.   

    那这四个静态方法你试一下:
    DateTime.FromFileTimeUtc()
    DateTime.FromOADate()
    DateTime.FromFileTime()
    DateTime.FromFileTimeUtc()如果都不对,就得研究一下原来的时间格式是什么了:)
      

  6.   

    使用楼上的办法
    long str=1125086636;
    DateTime t=DateTime.FromFileTimeUtc(str);
    MSG.Text=t.ToString();显示结果为:1601-1-1 0:01:52,而实际的应为:2005-9-3 05:35 PM估计php的time()写入的是unix的时间戳吧!?不解,望高手们帮帮忙!
      

  7.   

    unix时间戳(Unix Epoch)定义为从1970年起过去的秒数,所以应该这样转换:long str=1125086636;
    DateTime dt = new DateTime(1970,1,1);
    dt = dt.AddSeconds(str);
    Console.WriteLine(dt.ToString());你上面的实际时间应该是错误的,正确的是:2005-8-26 20:03:56
      

  8.   

    补充一下:unix时间戳(Unix Epoch)定义为从1970年1月1日0时起过去的秒数
      

  9.   

    经过测试,应该是:
    long str=long.Parse(time.Text.Trim());
    DateTime dt = new DateTime(1969,12,31,20,0,0);
    dt = dt.AddSeconds(str);
    this.Label1.Text=dt.ToString();