0

我是 C# 的初学者并编写了一些代码。但在那里出错。错误是:无法将类型字符串隐式转换为 System.date.time 但我在这里看到我在字符串中声明

  public partial class HDate
    {
        private string StartYear;

        public string StartYear1
        {
            get { return StartYear; }
            set { StartYear = value; }
        }
        private string EndYear;

        public string EndYear1
        {
            get { return EndYear; }
            set { EndYear = value; }
        }
        private string Year;

        public string Year1
        {
            get { return Year; }
            set { Year = value; }
        }
        private DateTime StartDate;

        public DateTime StartDate1
        {
            get { return StartDate; }
            set { StartDate = value; }
        }
        private DateTime EndDate;

        public DateTime EndDate1
        {
            get { return EndDate; }
            set { EndDate = value; }
        }
        private string StartMonth;

        public string StartMonth1
        {
            get { return StartMonth; }
            set { StartMonth = value; }
        }
        private string EndMonth;

        public string EndMonth1
        {
            get { return EndMonth; }
            set { EndMonth = value; }
        }
        private DateTime StartDay;

        public DateTime StartDay1
        {
            get { return StartDay; }
            set { StartDay = value; }
        }
        private DateTime EndDay;

        public DateTime EndDay1
        {
            get { return EndDay; }
            set { EndDay = value; }
        }


        public HDate() { }

        public HDate(){
        DateTime today = DateTime.Now;
        int year = today.Year;
        int month = today.Month;
        DateTime day = today;

        DateTime StartDate = (StartYear + StartMonth + StartDay);
        DateTime EndDate   = (EndYear + EndMonth + EndDay);

        if (month <= 6)
        {
            //string StartYear = Convert.ToString(year-1);
            string StartYear = (year - 1).ToString();
            string StartMonth = Convert.ToString(7);
            string EndYear = Convert.ToString(year);
            string EndMonth = Convert.ToString(6);
            string EndDay = Convert.ToString(30);
        }
        else if(month >= 7)

        {
            string StartYear =Convert.ToString(year);
            string StartMonth = Convert.ToString(7);
            string StartDay = Convert.ToString(1);
            string EndYear = Convert.ToString(year+1);
            string EndMonth = Convert.ToString(6);
            string EndDay = Convert.ToString(30);

        }
           return StartDate+';'+EndDate;
       }

这些数据将其返回到另一个函数..

4

2 回答 2

3

您已定义StartYear为私有字符串字段,与 StartMonth 相同,在 C# 中您不能执行以下操作:

DateTime StartDate = (StartYear + StartMonth + StartDay)

StartDay 是 DateTime 类型,我不确定您到底想做什么,看起来您正在尝试从 StartYear、StartMonth 和 StardDay 构造日期。

如果您可以将 StartDate、StartMonth 和 StartDay 转换为整数,那么您可以使用 DateTime 构造函数DateTime(Year,month,day);类似以下的东西;

    int StartYear = 2012;
    int StartMonth = 06;
    int StartDay = 15;

    DateTime dt = new DateTime(StartYear, StartMonth, StartDay);
于 2012-06-19T04:59:18.813 回答
0

我会假设这些可能不起作用:

DateTime StartDate = (StartYear + StartMonth + StartDay);
DateTime EndDate   = (EndYear + EndMonth + EndDay);

private string StartYear;
private string StartMonth;
private DateTime StartDay;

private string EndYear;
private string EndMonth;
private DateTime EndDay;

您可以做的唯一可能的“添加”(除非您重载运算符)是日期时间和时间跨度。 http://msdn.microsoft.com/en-us/library/system.datetime.op_addition(v=vs.90).aspx

年和月是字符串,最后一个是日期时间对象。您需要将它们转换为 int 并从中创建一个新的日期时间。像这样的东西:

DateTime StartDate = new DateTime(Int32.parse(StartYear), Int32.parse(StartMonth), StartDay.Day);
于 2012-06-19T05:05:28.833 回答