原c++的TDateTime是一个float的带小数的时间;现在用c#写该怎么写,如何在c#里获取这个float时间的的小数点前的值、小数点后的值。float tt = Convert.ToSingle(DateTime.Now);会提示 从“DateTime”到“Single”的强制转换无效。
DateTime.Now.Ticks 是个什么精度的值(比如:634489379727968750)

解决方案 »

  1.   

    TDateTime是VCL定义的,和C++没有关系。
    一般来说,这个数等于从某一天开始的天数,小数部分是小时、分、秒的余数,你可以参考VCL的文档。比如 1900年1月1日 0:00 开始计算,那么 1900.01.02 0.00 就是 1.0f。 1900.01.31 12:00 就是 30.5f。1900.01.31 18:00 就是 30.75f。有了这个理论,写程序就容易了。
    TimeSpan tp = new TimeSpan(DateTime.Now.Tick - 基数日期时间.Tick);
    float d = tp.Days;
    float h = tp.Hours - d * 24;
    float m = tp.Minutes - d * 24 * 60 - h * 60;
    float s = tp.Seconds - d * 24 * 3600 - h * 3600 - m * 60;
    float yourresult = d + h / 24.0 + m / 24.0 / 60.0 + s / 24.0 / 3600.0;
      

  2.   

    public   static   double   Timer() 
                             { 
                                         DateTime   t; 
                                         t   =   DateTime.Now; 
                                         return   (t.Hour   *   3600   +   t.Minute   *   60   +   t.Second   +   ((double)t.Millisecond)   /   1000); 
                             }
      

  3.   

    楼主,VC里面的是CTime,long lTime=tCurTime.GetTime();可以直接获取“GetTime will return the number of seconds between the current CTime object and January 1, 1970.”返回与1970,1,1相差的秒值,但是DateTime的模式跟CTime大不相同,如果要取得一致结果,就需要进行转换:        /// <summary>
            /// 获取时间的长整型值
            /// </summary>
            /// <returns></returns>
            public static uint GetLongTime(DateTime time)
            {
                DateTime time197011 = new DateTime(1970, 1, 1);
                //DateTime time = DateTime.Now;
                TimeSpan ts = time - time197011;
                TimeZone localZone = TimeZone.CurrentTimeZone;
                TimeSpan off = localZone.GetUtcOffset(time);
                ts -= off;            return (uint)ts.TotalSeconds;
            }         /// <summary>
            /// 转换时间格式
            /// </summary>
            /// <param name="time"></param>
            /// <returns></returns>
            public static DateTime ConverToTime(int time)
            {
                DateTime time197011 = new DateTime(1970, 1, 1);
                time197011 = time197011.AddSeconds(time);
                TimeZone localZone = TimeZone.CurrentTimeZone;
                TimeSpan ts = localZone.GetUtcOffset(time197011);
                time197011 += ts;            return time197011;
            }
      

  4.   

    “TDateTime是VCL定义的,和C++没有关系。”
    那他的基数时间是多少,记得好像是1799年还是多少来着?
      

  5.   

    TDateTime为实现TDateTime数据类型和使用TDateTime数据类型的日期-时间运行库例程的C++类。
           TDateTime类继承了double型的val数据成员,其中包含了日期-时间值。TDateTime值的整数部分为自从12/30/1899以来的天数。其小数部分为天的时间。
           下列为一些TDateTime值的例子以及对应的日期和时间:
           值       日期和时间
        0 12/30/1899 12:00 am
        2.75 1/ 1/1900 6:00 pm
        -1.25 12/29/1899 6:00 am
        35065 1/ 1/1996 12:00 am
           要计算两个日期间的天的小数部分,把两个值相减。要在某天的小数部分增加一个日期-时间值,可把该小数加上日期-时间值。
      

  6.   

    你完全可以模仿我上面写的,把TDateTime的double值与DataTime转换写出来