Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi there,

Well i have a big problem formatting the datetime variable to suit my needs and no matter how much i dag i havnt found the right solution.

I'll show you the code im writting and explain.
I have this method
C#
public static List<Reservation> GetReservationsReportBySites(List<Site> sites, DateTime fromDate, DateTime toDate, DateTime fromTime, DateTime toTime, ReservationStatus status)

The method recieve a few datetime variables, now what im trying to do with them is to define which reservations i want the application to show, in the database i have also 4 datetime two of them are from and to date without the actual hour and the other two are the from and to time without the actual hour (i cant change it, it and old database from work).
How do i make so that i will compare the dates and time the user pick with those four variables, i know i need to somehow break the datetime to date and time but im unable to do that.

this is the code of what i was thinking that will work to sort the reservations.
charp
var item in data.Where(p => p.StartDate.Value >=
                        fromDate.Date && p.StartDate.Value >=
                        fromTime && p.Valid == isValid &&
                                p.EndDate.Value<= toDate && p.EndDate.Value <= toTime))


as you can see im trying to use LINQ and im also learning it as i go so i'll take all the help i will get.

thanks in advance.
Posted
Updated 7-May-11 9:43am
v2
Comments
Jani Giannoudis 13-May-11 16:51pm    
Did you mean that one pair (start/end) contains the date-part and the other pair contains the time-part?

Already tried those two solutions and both didnt solve my problem, the thing is the end object that we created wont recieve timespan cause it's set to datetime.
when i tried it with the LINQ it didnt compile and gave me an error.

I even tried with four datetimepickers two for the fromdate and todate and two for fromtime and totime.

anyone know of another way to send a datetime variable with only the time or the date?

I tried every other solution i could find, i tried ticks, i tried converting to string and then convert back to datetime but that didnt work too cause it took the current date or time and it didnt suit my needs.

I'll take any other solution you can give me, i've become desperate.

Thanks again for the help.
 
Share this answer
 
Extracting the time or date from a datetime is possible. The following code shows a way how to extract the time from a date and how to remove the time from the date.

C#
DateTime now = DateTime.Now;
TimeSpan onlyTime = now - now.Date;
DateTime onlyDate = now - onlyTime;
Console.WriteLine(now);
Console.WriteLine(onlyTime);
Console.WriteLine(onlyDate);


Hope this helps.
 
Share this answer
 
Comments
Fabio V Silva 7-May-11 15:59pm    
There's no need for those calculations, you can use now.Date for the date part and now.TimeOfDay for the time part...

Either way, not sure that's the OP's problem...

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