C#中的数据转换出现问题 [英] Data Conversion in C# is making problem
问题描述
任何人都可以帮助我解决问题吗?
我在查找日期差时遇到问题,我想要两个日期之间的日期差,一个来自格式为(dd/mm/yyyy)的文本框,另一个来自数据库,所以这是我的问题,而来自数据基于它具有Datetime,所以我必须将来自文本框的日期转换为datetime,但是我只输入日期,所以我只需要将日期转换为dateformat,所以这里有任何函数可以将日期转换为仅日期和C#中的日期是我的代码.
can any one help me in solving my problem ?
i have problem in find the date difference , i want date difference between two dates, one is coming from text box which is in format(dd/mm/yyyy)and other is coming from the Database so here is my problem while coming from data base it is having Datetime so i have to convert the date coming from text box into datetime but i am entering only date so i need to convert the date only in dateformat so is there any function to convert date into only date with date in C# here is my code.
decimal LoanAmt = Convert.ToDecimal(RepayloanAmountLbl.Text);
int Tenure = Convert.ToInt32(RepayTenureLbl.Text);
DateTime RepayStartDate = Convert.ToDateTime(StartDateTxt.Text);
//DateTime DisbursementDate = Convert.ToDateTime(RepayDateLbl.Text);
DateTime DisbursementDate = Convert.ToDateTime(dt.Rows[0]["REPAY_START_DATE"].ToString());
TimeSpan ts = RepayStartDate.Subtract(DisbursementDate);
int days = ts.Days;
decimal Rate = Convert.ToDecimal(RepayInterestLbl.Text) / 100;
decimal interest = ((LoanAmt * Rate) / 365) * days;
decimal X = (decimal)Math.Round(interest);
IntAmtTxt.Text = Convert.ToString(X);
ActualIntLbl.Text = Convert.ToString(X);
PrinAmtTxt.Enabled = false;
请帮助我解决问题,这是我工作的绊脚石
please help me in solving the problem it is the stoper for my work
推荐答案
看看DateTime.Date
http://msdn.microsoft.com/en-us/library/system.datetime. date.aspx [^ ]
Have a look at DateTime.Date
http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx[^]
// This would be date + time
DateTime now = DateTime.Now;
// This is just date
now.Date
还是总有ToShortDateString
http://msdn.microsoft.com/en-us/library/system.datetime. toshortdatestring.aspx [ ^ ]
Or there''s always ToShortDateString
http://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring.aspx[^]
您不能仅将日期转换为DateTime.那会是几月几号?您确定要使用默认的月份和年份吗?您可能会假设当前的月份和年份,对吗?
如果假设当前月份和年份,则可以执行以下操作.
You can not convert ONLY date to DateTime. What month and year would that be? Are you sure you want to use the default month and year? You may be assuming the current month and year, are you?
If you are assuming current month and year, you could do something like this.
DateTime RepayStartDate = DateTime.Now;
RepayStartDate .Month = Convert.ToInt32(StartDateTxt.Text);
将文本字符串转换为DateTime
对象后,应确保两者包含相同的时间值.将每个中的时间设置为零将确保您的时间为整天.
When you have converted your text string to a DateTime
object you should ensure that both contain the same time value. Setting the times in each to zero would ensure that your timespan will be a full number of days.
这篇关于C#中的数据转换出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!