如何在C#windowsform中使用int和datetime [英] How to work with int and datetime in C# windowsform

查看:168
本文介绍了如何在C#windowsform中使用int和datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在申请一个申请。我希望用户能够输入他们想要借入的贷款金额,如果贷款在1年内,它应该有效。所以取决于用户借入贷款的月份。对于6个月或更短的程序,程序应该在六到九个月内将用户输入的数量乘以5%的7.5%用户输入e.tc。所以如果datatimepicker(天)的差异小于6个月(之前),但我收到错误



我尝试过:



Good day everyone please i am working on an application. i want the user to be able to enter the amount of loan they want to borrow, and if the loan is within 1 year it should work. so depending on the months the user is going to borrow the loan. for 6months or lesser the program should multiply the amount entered by the user by 5% within six - nine months 7.5% of the money the user enter e.tc. so if the datatimepicker(days) difference is lesser than 6months(before) but i am getting error

What I have tried:

int result; 
DateTime reqdt = dtrequest.Value.Date;
DateTime retdt = dtreturn.Value.Date;

DateTime before = reqdt.AddMonths(6);
DateTime within = reqdt.AddMonths(9);
DateTime after = reqdt.AddYears(1);
TimeSpan ts = retdt - reqdt;

int days = ts.Days;
DateTime cde = new DateTime(days);//................int days to time 
// DateTime cde = Convert.ToDateTime(days);
result = Convert.ToInt32(txtamount.Text);//....................converting string to int(error not in the correct format)

if (cde <= before)
{
   double mult = result * 5;
   txtresult.Text = mult.ToString();
}

推荐答案

使用int.TryParse [ ^ ]。你从方法调用中得到一个true / false值,告诉你解析操作是否成功。



现在,你不能将整数转换为DateTime。这样做没有任何意义。



但是,您可以添加小时,分钟,秒,天,月......等等日期/时间值,但必须有一个开始日期/时间,就像你已经在代码片段顶部附近做的那样。



在你的代码片段,您可以在对某些现有DateTime值的.AddDays()调用中使用ts.Days值。
Use int.TryParse[^] instead. You get a true/false value back from the method call telling you whether the parsing operation succeeded or not.

Now, you cannot convert an integer to a DateTime. It doesn't make any sense to do so.

You CAN, however, add hours, minutes, seconds, days, months, ...whatever to an existing date/time value but that has to have a starting date/time to work with, like you're already doing near the top of your code snippet.

In your code snippet, you can use the ts.Days value in a call to .AddDays() on some existing DateTime value.


这篇关于如何在C#windowsform中使用int和datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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