C#,需要代码帮助 [英] C#, need help with code

查看:104
本文介绍了C#,需要代码帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我现在有另外一个与Dates相同项目的问题。

我需要实现关于酒店住宿时间的代码并将其用于条例草案中的总数。



我编写代码的方式无法正常工作,我将日期设置为30/05/2018到06/02/2018,但如果设置为1则工作正常/ 05 / 2018-10 / 05 / 2018.





谢谢。



我尝试过:



Hi,
I have another question related to the same project about Dates now.
I need to implement the code about length of stay in hotel and use it for an totalAmount in the Bill.

The way I wrote my code not working properly, I set up dates as 30/05/2018 to 06/02/2018, but is working if set up 1/05/2018-10/05/2018.


Thank you.

What I have tried:

DateTime DateIn, DateOut;
           int TotalDays;

           DateIn = Convert.ToDateTime(entryDatePicker.Value);
           DateOut = Convert.ToDateTime(depDatePicker.Value);

           TotalDays =(DateOut-DateIn).Days;

推荐答案

问题看起来是DateTime格式;您提供的4个日期中有3个以mm / dd / yyyy格式和dd / mm / yyyy格式有效。



您可能想尝试使用DateTime .ParseExact方法:

The problem looks to be in the DateTime format; 3 of the 4 dates you give are valid in either the mm/dd/yyyy format and as dd/mm/yyyy format.

You may want to try using DateTime.ParseExact method:
DateTime dt=DateTime.ParseExact("24/01/2013", "dd/MM/yyyy", CultureInfo.InvariantCulture);


DateTimePickerValue是一个DateTime;因此无需转换它。



Convert.ToDateTime唯一不接受的是:另一个DateTime!
"DateTimePicker" "Value" is ALREADY a DateTime; so no need to convert it.

And the only thing that Convert.ToDateTime won't accept is: ANOTHER DateTime!


这篇关于C#,需要代码帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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