views:

674

answers:

8

I use this to convert DateTime value into Date and then I add 00:00:00 and 23:59:59 to make sure whole day is taken into consideration when counting stuff. I'm pretty sure it's wrong way of doing things. What would be the right way?

        DateTime varObliczOd = DateTime.Parse(dateTimeWycenaPortfelaObliczDataOd.Value.ToShortDateString() + " 00:00:00");
        DateTime varObliczDo = DateTime.Parse(dateTimeWycenaPortfelaObliczDataDo.Value.ToShortDateString() + " 23:59:59");
+5  A: 

DateTime objects have a Date property which might be what you need.

Software.Developer
A: 

Something like this maybe? I've typed this out of my head, there are probably some mistakes in the code.

DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.AddSeconds(-dateTimeWycenaPortfelaObliczDataOd.Seconds).AddMinutes(-dateTimeWycenaPortfelaObliczDataOd.Minutes).AddHours(-dateTimeWycenaPortfelaObliczDataOd.Hours);

DateTime varObliczDo  = new DateTime(dateTimeWycenaPortfelaObliczDataDo.Year, dateTimeWycenaPortfelaObliczDataDo.Month, dateTimeWycenaPortfelaObliczDataDoDay, 23, 59, 59);
Gerrie Schenck
A: 

Hi,

DateTime newDate = new DateTime( oldDate.Year, oldDate.Month, oldDate.Day, 23, 59,59 )
DateTime newDate = new DateTime( oldDate.Year, oldDate.Month, oldDate.Day, 0, 0, 0 )
IordanTanev
+10  A: 

if dateTimeWycenaPortfelaObliczDataOd is of type DateTime, You can use:

dateTimeWycenaPortfelaObliczDataOd.Date

to get the date part only (time will be 00:00:00...). If you want to get the very last tick of the date, you can use:

dateTimeWycenaPortfelaObliczDataOd.Date.AddDays(1).AddTicks(-1)

but you really better work with the next date (.AddDays(1)).

In any case, there is no need to convert to string and back to DateTime.

M.A. Hanin
Great, I can't work with .AddDays(1) only because it will take into consideration next day transactions (some stuff is written in db done on 00:00:00)
MadBoy
What i meant is that if you use the dates for range check, you can pull all maching records whose timestamp complies with: StartOfTomorrow > timestamp >= startOfToday .Working with the last tick of the day should also work well, it just smells bad.
M.A. Hanin
True, i should have used timestamp < startOfTommorow to make sure 00:00 isn't used for the next day.
MadBoy
+1  A: 

You could use the Date property of the DateTime object to accomplish what you need.

DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Value.Date;
DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataDo.Value.Date.AddDays(1);

If you really want it to end at 23:59:59 you can do:

DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataDo.Value.Date.AddDays(1).AddSeconds(-1);

Will set varObliczDo to be your ending date with no time plus one day (at midnight). So if dateTimeWycenaPortfelaObliczDataDo was 2010-03-05 16:12:12 it would now be 2010-03-06 00:00:00.

Joshua
A: 

You could work with TimeSpan:

DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd - new TimeSpan(dateTimeWycenaPortfelaObliczDataOd.Hours, dateTimeWycenaPortfelaObliczDataOd.Minutes, dateTimeWycenaPortfelaObliczDataOd.Seconds);

Like that you avoid at least the parsing, which can fail depending on the local culture settings.

Enyra
Yes it failed on one workstation and i knew it will be that. I used accepted solution as it seemed better.
MadBoy
Which solution you take is a matter of preferences, your accepted solution is nice to read in code, but it uses several operations to calculate the result while mine uses just one. Of course, what is exactly behind this one operation I also can not tell you ^^
Enyra
+2  A: 

You can use the following properties / methods on a DateTime object to get your values :

DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Date;
DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataOd.AddDayes(1).AddTicks(-1);
Thibault Falise
+2  A: 

It would help to know why you're needing it, but this would work.

DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Date;
DateTime varObliczDo = varObliczOd.AddDays(1).AddSeconds(-1);

Using the Date attribute and then manipulating them directly to create the required time component - no need to bother with parsing and conversion.

Unsliced