从日期时间选择器计算年龄

编程入门 行业动态 更新时间:2024-10-28 00:15:08
本文介绍了从日期时间选择器计算年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个日期时间选择器和一个文本框。 我希望当我从日期时间选择器中选择日期时,年龄将自动计算我从中选择的日期日期时间选择器和年龄显示在该文本框中。

I have one datetime picker and one textbox. I want that when i select date from datetime picker the age will automatically calculate acording to date which i select from datetime picker and the age is display in that textbox.

推荐答案

// dt1 is the date picked by the user TimeSpan ts = DateTime.Now - dt1;

msdn.microsoft/en-us/library/system.timespan。 aspx [ ^ ]

试试这个样本并使其适应你的表格: Try this sample and adapt it to your form: using System; public class Program { public static void Main() { //DateTime dob = dateTimePicker1.Value; // assuming dateTimePicker1 is used DateTime dob = new DateTime(1995, 2, 10); DateTime today = DateTime.Today; int age = today.Year - dob.Year; if (dob.AddYears(age) > today) { age--; } Console.WriteLine(age); } }

参考: DateTimePicker.Value Property [ ^ ]

更多推荐

从日期时间选择器计算年龄

本文发布于:2023-10-18 11:44:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504137.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:年龄   日期   时间   选择器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!