Click here to Skip to main content
15,867,865 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
Posted
Updated 19-Jun-18 11:36am
v2
Comments
F-ES Sitecore 19-Jun-18 10:15am    
"Not working" doesn't give anyone enough information to be able to help you. Would you phone a mechanic and say "My car isn't working, how do I fix it?"

At a guess this is a localisation issue regarding the format your OS assumes your dates are in, it has to assume a format because you haven't explicitly given one, and it can't read your mind. How does it know if "30/5" is the 30th day of the 5th month or the 5th day of the 30th month?
Bryian Tan 19-Jun-18 10:21am    
Try it with MM/dd/yyyy format.

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:
C#
DateTime dt=DateTime.ParseExact("24/01/2013", "dd/MM/yyyy", CultureInfo.InvariantCulture);
 
Share this answer
 
"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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900