如何确定datetime的值是有效(完整日期)还是只有年份?

时间:2017-01-04 02:55:48

标签: c# datetime

DATETIME Value 1980

我有这段代码从sql中检索DateTime

string startDate = ed.IDAFrom != null ? Convert.ToDateTime(ed.IDAFrom).ToShortDateString() : "";

修改

注意:此处的日期为年级毕业(必须为完整日期)

我的视图中有一个日期选择器(所以日期是完整的日期吗?)..如果用户忘记了毕业日期,那么用户将把日期选择器编辑为仅年份。

我想在这里实现的是,如果用户编码完整日期或年份,如果用户编码完整日期,我希望获得YEAR的值。

希望它能够清除。

感谢。

代码

foreach (var ed in exams)
        {
            int rowId = i;
            string startDate = ed.IDAFrom != null ?Convert.ToDateTime(ed.IDAFrom).ToShortDateString() : "";
            string endDate = ed.IDATo != null ? Convert.ToDateTime(ed.IDATo).ToShortDateString() : "";
            string InclusiveDates = startDate + " - " + endDate;
           rowsObj[i] = new { id = rowId, cell = new object[] { rowId, InclusiveDates } };
            i++;
        }

1 个答案:

答案 0 :(得分:2)

  

我只想在完整日期显示年份。

DateTime value;

if(DateTime.TryParse(ed.IDAFrom, out value))
{
    Int year = value.Year;
}

如果你关心文化:

CultureInfo = CultureInfo.CreateSpecificCulture("en-US"); // Or whichever culture you need
if (DateTime.TryParse(ed.IDAFrom, culture, DateTimeStyles.None, out value))
{
    int year = value.Year;
}

如果用户可以选择输入年份或完整日期,请使用以下方法:

public static string GetDateTime(string value)
{
    DateTime date;
    string dateString = ""; // Empty by default

    // If full date is given, this will succeed
    if (DateTime.TryParse(value, out date))
    {
        dateString = date.ToShortDateString();
    }
    // If only year is given then this will succeed
    else if (DateTime.TryParseExact(value,
            "yyyy",
            CultureInfo.InvariantCulture,
            DateTimeStyles.None,
            out date))
    {
        dateString = date.ToShortDateString();
    }

    return dateString;
}

修改

现在您已经在问题中添加了更多代码,以下是使用Linq:

的方法
int i = 0;
int j = 0;
var rows = list.Select(exam =>
{

    string inclusiveDates = string.Format("{0} - {1}", GetDateTime(exam.IDAFrom), GetDateTime(exam.IDATo));
    return new
    {
        Id = ++i,
        Cell = new object[] { ++j, inclusiveDates }
    };
})
.ToList();

这是一个示例用法

class Program
{
    public class Exam
    {
        public string IDAFrom { get; set; }
        public string IDATo { get; set; }
    }

    public static string GetDateTime(string value)
    {
        DateTime date;
        string dateString = ""; // Empty by default

        // If full date is given, this will succeed
        if (DateTime.TryParse(value, out date))
        {
            dateString = date.ToShortDateString();
        }
        // If only year is given then this will succeed
        else if (DateTime.TryParseExact(value,
                "yyyy",
                CultureInfo.InvariantCulture,
                DateTimeStyles.None,
                out date))
        {
            dateString = date.ToShortDateString();
        }

        return dateString;
    }

    static void Main(string[] args)
    {

        var list = new List<Exam> { new Exam { IDAFrom = "1999", IDATo = null },
        new Exam { IDAFrom = DateTime.Now.ToShortDateString(), IDATo = DateTime.Now.AddDays(5).ToShortDateString() } };
        int i = 0;
        int j = 0;
        var rows = list.Select(exam =>
        {

            string inclusiveDates = string.Format("{0} - {1}", GetDateTime(exam.IDAFrom), GetDateTime(exam.IDATo));
            return new
            {
                Id = ++i,
                Cell = new object[] { ++j, inclusiveDates }
            };
        })
        .ToList();


        foreach (var item in rows)
        {
            Console.WriteLine("{0}\t{1}\t{2}", item.Id.ToString(), item.Cell[0], item.Cell[1]);
        }
        Console.Read();

    }
}