Calculate # of Years Alive in C# WinForm

This question already has an answer here:

  • How do I calculate someone's age in C#? 64 answers

  • Try something like this:

    private void button1_Click(object sender, EventArgs e)
    {
        DateTime  sonsBirthday = DateTime.Parse(txtSonsBirthday.Text).Date;
    
        DateTime now =  DateTime.Now;
    
        TimeSpan timeSpan = now - sonsBirthday;
        //timeSpan = Convert.TimeSpan(lblTimeAlive); // old
        lblTimeAlive.Text = timeSpan.ToString(); // new
    

    Then fine tune the string formatting for timeSpan .

    链接地址: http://www.djcxy.com/p/54368.html

    上一篇: 从C#中的文本框中的日期计算年龄

    下一篇: 计算C#WinForm中的活跃年数