简体   繁体   中英

How to convert int to DateTime

My plan is to make Day, Month, Year, Hours, StartMinute a dropdown and the selected numbers of the dropdown is gonna be converted to a datetime and saved in the database

How to convert int to DateTime, right now i get a error in my StartTime. The error is "Sannot implictly convert type 'int' to 'System.DateTime'".

public class Job
{
    public int ID { get; set; }

    public int Day { get; set; }
    public int Month { get; set; }
    public int Year { get; set; }
    public int StartHour { get; set; }
    public int StartMinute { get; set; }
    public int EndHour { get; set; }
    public int EndMinute { get; set; }

    public DateTime StartTime
    {
        get
        {
            return  Day + Month + Year + StartHour + StartMinute;
        }

        set
        {
            StartTime = Day + Month + Year + StartHour + StartMinute;
        }
    }
    public DateTime EndTime { get; set; }

This is pritty simple if you have all the values as seperated Dropdowns:

int year = 2016, month = 2, day = 25, hour = 10, minute = 41;
DateTime date = new DateTime(year, month, day, hour, minute, 0);

The last 0 in the "new DateTime " is for the seconds-part

Edit for System.ArgumentOutOfRangeException in the getter:

    public DateTime StartTime
    {
        get
        {
            if (Year < 1) Year = 1;
            if (Month < 1) Month = 1;
            if (Day < 1) Day = 1;
            if (StartHour < 0) StartHour = 0;
            if (StartMinute < 0) StartMinute = 0;
            return new DateTime(Day, Month, Year, StartHour, StartMinute, 0);
        }
        set
        {
            Day = value.Day;
            Month = value.Month;
            Year = value.Year;
            StartHour = value.Hour;
            StartMinute = value.Minute;
        }
    }

使用DateTime的构造方法之一。

You are just summing up some int and expecting the framework to recognize it as a DateTime . Your problem is, that the framework does not understand what you mean with the name Year and Hour .

Use the DateTime(int year, int month, int day, int hour, int minute, int second) constructor instead. In the way

StartTime = new DateTime(Year, Month, Day, StartHours, StartMinutes, 0)

Also I don't think to put this in a getter-function is not the right way because you are waiting for some user input. So you have to wait for an event to happen (changing one of the drop-downs or klicking on a button) before the variable StartTime can be set. Therefore, you probably have to set StartTime in a method.

There is no direct conversion from number, or multiple numbers to structure (as DateTime is a structure ).

You have multiple constructors for DateTime . Number 6 ot of 12 says VS:

public DateTime( int year, int month, int day, int hour, int minute, int second )

You can use the DateTime constructor:

DateTime(Int32, Int32, Int32, Int32, Int32, Int32)
Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second.

public class Job
{
    public int ID { get; set; }
    public int Day { get; set; }
    public int Month { get; set; }
    public int Year { get; set; }
    public int StartHour { get; set; }
    public int StartMinute { get; set; }
    public int EndHour { get; set; }
    public int EndMinute { get; set; }

public DateTime StartTime
{
    get
    {
        return new DateTime(Year, Month, Day, StartHours, StartMinutes, 0);
    }
    set
    {
        Day = value.Day;
        Month = value.Month;
        Year = value.Year;
        StartHour = value.Hour;
        StartMinute= value.Minute;
    }
}
public DateTime EndTime { get; set; }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM