传入1988生成 1988-1-1
传入1998-3 生成1998-3-1
类似这样

解决方案 »

  1.   

    如果认为DateTime 类的构造函数不行,则实现如下三个重载版本,呵呵。private DateTime GetDateTime(int year)
    {
       DateTime dt = new DataTime(year,1,1);
       return dt;
    }private DateTime GetDateTime(int year, int month)
    {
       DateTime dt = new DateTime(year, month, 1);
       return dt;
    }private DateTime GetDateTime(int year, int month, int day)
    {
       DateTime dt = new Datetime(year, month, day);
       return dt;
    }
      

  2.   

    string[] formats = { "yyyy", "yyyy-M", "yyyy-M-d" };
    DateTime d1 = DateTime.ParseExact("1988", formats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);
    DateTime d2 = DateTime.ParseExact("1998-3", formats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);
    DateTime d3 = DateTime.ParseExact("1999-3-4", formats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);
    Console.WriteLine(d1.ToString("yyyy-M-d"));
    Console.WriteLine(d2.ToString("yyyy-M-d"));
    Console.WriteLine(d3.ToString("yyyy-M-d"));
      

  3.   

    private string GetYearMonthDay(string yearMonth)
    {
    if(yearMonth.IndexOf("-")>-1)
    return yearMonth+"-1";
    else return yearMonth+"-1-1";
    }