部分计算日期。 [英] Calculation a date in part.

查看:86
本文介绍了部分计算日期。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些代码来计算两个日期,分为四个部分,分别为年,月,周和日。如果有人的DOB是15-12-1988我想表现出30年,2个月,1天。它没有正确回应。请提供一些其他代码来解决它



我尝试过:



I am writing some code to calculate two dates in four parts as years, months, weeks, and days viz; if someone's DOB is 15-12-1988 I want to show this like 30 years,2 months, 1 day. It does not response properly. Please provide me some other codes to solve it

What I have tried:

double months;
     double years;
     //double weeks;
     double days;
     DateTime inTime = Convert.ToDateTime(dateTimePicker1.Text);
     DateTime outTime = Convert.ToDateTime(dateTimePicker2.Text);
     if (outTime >= inTime)
     {
         textBox3.Text = outTime.Subtract(inTime).Days.ToString() + " days ";

         years = (outTime - inTime).Days / 365;
         months = (outTime - inTime).Days / 12;
         days = (outTime - inTime).Days / 30;
         textBox2.Text = Convert.ToInt32(years).ToString() + " Years," + Convert.ToInt32(months).ToString() + " Months," + Convert.ToInt32(days).ToString() + " Days" ;


     }

推荐答案

请看这里:使用Age:它与TimeSpan不同! [ ^ ]


为什么要使用datetimepickers中的文本字段,然后需要将其转换为DateTime值?首先使用Value属性。 TimeSpan以来,您也不需要所有这些调整结构(系统)| Microsoft Docs [ ^ ]可以自动提供。请参阅上面的OriginalGriff答案。
Why are you using the text field from the datetimepickers which you then need to convert to DateTime values? Use the Value property in the first place. You also do not need all those calulations since the TimeSpan Struct (System) | Microsoft Docs[^] can provide them automatically. See OriginalGriff's answer above.


这篇关于部分计算日期。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆