如何在文本框中插入日期之前的年份

本文关键字:日期 插入 文本 | 更新日期: 2023-09-27 18:14:29

我有2个文本框,fromdate和todate。我必须在todate文本框中插入今天的日期,在fromdate文本框中插入2年前的日期。我该怎么做呢?

    DateTime todate = DateTime.Today;
    string TodateString = todate.ToString();
    string fromdate ;
    //from date = todate - 2 years
    todatetextbox.Text = todate;
    fromdatetextbox.Text = fromdate;

如何在文本框中插入日期之前的年份

date = myDate.AddYears(-2);

来源:如何从日期时间减去一年?

简单地使用Year和一个简单的数学运算::

        DateTime todate = DateTime.Today;
        int year2 = todate.Year-2;
        string DesiredYear = year2.ToString();