简体   繁体   English

错误 CS0029:无法将类型“System.DateTime”隐式转换为“int”(CS0029)(DeclaringConstructor)

[英]Error CS0029: Cannot implicitly convert type 'System.DateTime' to 'int' (CS0029) (DeclaringConstructor)

Whats wrong here, its code from book but not working?这里有什么问题,它的代码来自书中但不起作用?

using System;

namespace DeclaringConstructor
{
    public class Time
    {
        int Year;
        int Month;
        int Date;
        int Hour;
        int Minute;
        int Second;

        public void DisplayCurrentTime()
        {
            System.Console.WriteLine("{0}/{1}/{2}/{3}/{4}/{5}", Month, Date, Year, Hour, Minute, Second);
        }

        //konstruktor
        public Time(System.DateTime dt)
        {
            Year = dt.Year;
            Month = dt.Month;
            Date = dt.Date;
            Hour = dt.Hour;
            Minute = dt.Minute;
            Second = dt.Second;

        }
    }
    public class Tester
    {
        static void Main()
        {
            System.DateTime currentTime = System.DateTime.Now;
            Time t = new Time(currentTime);
            t.DisplayCurrentTime();

        }
    }
}

the problem is here:问题在这里:

 Date = dt.Date;

you need the Day property of Datetime .你需要DatetimeDay属性。 Try like:试试看:

Day = dt.Day;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误CS0029:无法将类型&#39;int&#39;隐式转换为&#39;Score&#39; - error CS0029: Cannot implicitly convert type 'int' to 'Score' 错误 CS0029:无法将类型“string”隐式转换为“int” - Error CS0029: Cannot implicitly convert type 'string' to 'int' 错误 CS0029:无法将类型“int”隐式转换为“string” - Error CS0029: Cannot implicitly convert type 'int' to 'string' CS0029:无法将类型&#39;int&#39;隐式转换为&#39;bool&#39; - CS0029: Cannot implicitly convert type 'int' to 'bool' 错误:CS0029 无法隐式转换类型 'System.Collections.Generic.List<int> '到'X'?</int> - Error : CS0029 Cannot implicitly convert type 'System.Collections.Generic.List<int>' to 'X'? 错误 CS0029 无法将类型“字符串”隐式转换为“双精度” - Error CS0029 Cannot implicitly convert type 'string' to 'double' 错误CS0029无法将类型“字符串”隐式转换为“十进制” - Error CS0029 Cannot implicitly convert type 'string' to 'decimal' 错误CS0029:无法将类型&#39;int&#39;隐式转换为&#39;bool&#39;-Unity3D - error CS0029: Cannot implicitly convert type `int' to `bool' - Unity3D 编译器错误消息:CS0029:无法将类型“int”隐式转换为“string” - Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to 'string' C# - 错误 CS0029 无法将类型“int”隐式转换为“Core.Models.Mandate” - C# - Error CS0029 Cannot implicitly convert type 'int' to 'Core.Models.Mandate'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM