Ignore Time element of DateTime?

  • Thread starter Thread starter Paolo
  • Start date Start date
P

Paolo

I have a series of queries which select transactions (using a Transaction
date) within a specified date range, where the start and end dates
(inclusive) are selected from a DateTimePicker control thus:

DateTime qryStart;
DateTime qryEnd;
.....
qryStart = dtmpickFrom.Value;
qryEnd = dtmpickTo.Value;

var query =
from trans in dataSet.Transaction
where ((trans.T_Date >= qryStart)
&& (trans.T_Date <= qryEnd)) ....

I've just done my analysis for January 2009. I have a number of transactions
with dates of 01/01/2009 then more with later dates. I'm finding that with
qryStart = 01/01/2009 the transactions with that date are being omitted from
the analysis. Looking at qryStart in the debugger it shows '01/01/09
14:49:52' (where the time reflects the time at which I selected the date in
the DateTime Picker to do the analysis.) It just so happens that the
transactions with a date of 01/01/09 all have times earlier than 14:49:52 so,
presumably, this explains why they don't appear in the analysis.

I don't actually use the time part of the DateTime variables so is there any
way I can get the queries to ignore it?
 
I have a series of queries which select transactions (using a
Transaction date) within a specified date range, where the start and end
dates (inclusive) are selected from a DateTimePicker control thus:

DateTime qryStart;
DateTime qryEnd;
.....
qryStart = dtmpickFrom.Value;
qryEnd = dtmpickTo.Value;

var query =
from trans in dataSet.Transaction
where ((trans.T_Date >= qryStart)
&& (trans.T_Date <= qryEnd)) ....

I've just done my analysis for January 2009. I have a number of
transactions with dates of 01/01/2009 then more with later dates. I'm
finding that with qryStart = 01/01/2009 the transactions with that date
are being omitted from the analysis. Looking at qryStart in the debugger
it shows '01/01/09 14:49:52' (where the time reflects the time at which
I selected the date in the DateTime Picker to do the analysis.) It just
so happens that the transactions with a date of 01/01/09 all have times
earlier than 14:49:52 so, presumably, this explains why they don't
appear in the analysis.

I don't actually use the time part of the DateTime variables so is there
any way I can get the queries to ignore it?



add 1 day timespan and use < operator.
 
Paolo said:
I have a series of queries which select transactions (using a Transaction
date) within a specified date range, where the start and end dates
(inclusive) are selected from a DateTimePicker control thus:

DateTime qryStart;
DateTime qryEnd;
.....
qryStart = dtmpickFrom.Value;
qryEnd = dtmpickTo.Value;

var query =
from trans in dataSet.Transaction
where ((trans.T_Date >= qryStart)
&& (trans.T_Date <= qryEnd)) ....

I've just done my analysis for January 2009. I have a number of transactions
with dates of 01/01/2009 then more with later dates. I'm finding that with
qryStart = 01/01/2009 the transactions with that date are being omitted from
the analysis. Looking at qryStart in the debugger it shows '01/01/09
14:49:52' (where the time reflects the time at which I selected the date in
the DateTime Picker to do the analysis.) It just so happens that the
transactions with a date of 01/01/09 all have times earlier than 14:49:52 so,
presumably, this explains why they don't appear in the analysis.

I don't actually use the time part of the DateTime variables so is there any
way I can get the queries to ignore it?

No, you can't, a DateTime value always has a time component. However,
you can set the time component to be zero:

qryStart = dtmpickFrom.Value.Date;
qryEnd = dtmpickTo.Value.Date;

If you want the end date to be inclusive, you should add one day to it:

qryEnd = dtmpickTo.Value.Date.AddDays(1);

In the query you should use the < operator to compare the end date. The
< operator is the exact complement to the >= operator, so that you will
get time ranges where no record will appear in two consecutive ranges.

var query =
from trans in dataSet.Transaction
where ((trans.T_Date >= qryStart)
&& (trans.T_Date < qryEnd)) ....
 
Paolo said:
I have a series of queries which select transactions (using a Transaction
date) within a specified date range, where the start and end dates
(inclusive) are selected from a DateTimePicker control thus:

DateTime qryStart;
DateTime qryEnd;
.....
qryStart = dtmpickFrom.Value;
qryEnd = dtmpickTo.Value;

var query =
from trans in dataSet.Transaction
where ((trans.T_Date >= qryStart)
&& (trans.T_Date <= qryEnd)) ....

I've just done my analysis for January 2009. I have a number of
transactions
with dates of 01/01/2009 then more with later dates. I'm finding that with
qryStart = 01/01/2009 the transactions with that date are being omitted
from
the analysis. Looking at qryStart in the debugger it shows '01/01/09
14:49:52' (where the time reflects the time at which I selected the date
in
the DateTime Picker to do the analysis.) It just so happens that the
transactions with a date of 01/01/09 all have times earlier than 14:49:52
so,
presumably, this explains why they don't appear in the analysis.

I don't actually use the time part of the DateTime variables so is there
any
way I can get the queries to ignore it?

I think you can chop the time off with a Tostring(format) and still do the
comparison.

var query =
from trans in dataSet.Transaction
where ((trans.T_Date.ToString("yyyyMMdd") >=
qryStart.Tostring("yyyMMdd")
&& (trans.T_Date.ToString("yyyyMMdd" <=
qryEnd.ToString('yyyyMMdd")) ....


I think there is a ToShortDate() which will chop off the time, and you
might be able to get away ToShortDate("yyyyMMdd") too.
 
eGoran: thank you. That has solvd my problem

Göran Andersson said:
No, you can't, a DateTime value always has a time component. However,
you can set the time component to be zero:

qryStart = dtmpickFrom.Value.Date;
qryEnd = dtmpickTo.Value.Date;

If you want the end date to be inclusive, you should add one day to it:

qryEnd = dtmpickTo.Value.Date.AddDays(1);

In the query you should use the < operator to compare the end date. The
< operator is the exact complement to the >= operator, so that you will
get time ranges where no record will appear in two consecutive ranges.

var query =
from trans in dataSet.Transaction
where ((trans.T_Date >= qryStart)
&& (trans.T_Date < qryEnd)) ....
 
Mr Arnold and Ken Foskey: thank you for your suggestions. I have actually
implemented Goran's suggestion but I was also wondering if it was possible to
'chop' off the Time element or at least set it to zero.
 
Please, foelks, don't do SILLY stuff like string conversions when all is needed it a little arithmetic...

static DateTime DatePart(DateTime dateTime)
{
return new DateTime(dateTime.Ticks - dateTime.Ticks % 864000000000L);
}

Cheers,
Axel




Paol wrote:

Ignore Time element of DateTime?
01-Feb-09

I have a series of queries which select transactions (using a Transaction
date) within a specified date range, where the start and end dates
(inclusive) are selected from a DateTimePicker control thus

DateTime qryStart
DateTime qryEnd
....
qryStart = dtmpickFrom.Value
qryEnd = dtmpickTo.Value

var query =
from trans in dataSet.Transactio
where ((trans.T_Date >= qryStart
&& (trans.T_Date <= qryEnd)) ...

I've just done my analysis for January 2009. I have a number of transactions
with dates of 01/01/2009 then more with later dates. I'm finding that with
qryStart = 01/01/2009 the transactions with that date are being omitted from
the analysis. Looking at qryStart in the debugger it shows '01/01/09
14:49:52' (where the time reflects the time at which I selected the date in
the DateTime Picker to do the analysis.) It just so happens that the
transactions with a date of 01/01/09 all have times earlier than 14:49:52 so,
presumably, this explains why they don't appear in the analysis

I don't actually use the time part of the DateTime variables so is there any
way I can get the queries to ignore it?

Previous Posts In This Thread:

Ignore Time element of DateTime?
I have a series of queries which select transactions (using a Transaction
date) within a specified date range, where the start and end dates
(inclusive) are selected from a DateTimePicker control thus

DateTime qryStart
DateTime qryEnd
....
qryStart = dtmpickFrom.Value
qryEnd = dtmpickTo.Value

var query =
from trans in dataSet.Transactio
where ((trans.T_Date >= qryStart
&& (trans.T_Date <= qryEnd)) ...

I've just done my analysis for January 2009. I have a number of transactions
with dates of 01/01/2009 then more with later dates. I'm finding that with
qryStart = 01/01/2009 the transactions with that date are being omitted from
the analysis. Looking at qryStart in the debugger it shows '01/01/09
14:49:52' (where the time reflects the time at which I selected the date in
the DateTime Picker to do the analysis.) It just so happens that the
transactions with a date of 01/01/09 all have times earlier than 14:49:52 so,
presumably, this explains why they don't appear in the analysis

I don't actually use the time part of the DateTime variables so is there any
way I can get the queries to ignore it?

Re: Ignore Time element of DateTime?
add 1 day timespan and use < operator.

Re: Ignore Time element of DateTime?
Paolo wrote

No, you can't, a DateTime value always has a time component. However,
you can set the time component to be zero

qryStart = dtmpickFrom.Value.Date
qryEnd = dtmpickTo.Value.Date

If you want the end date to be inclusive, you should add one day to it

qryEnd = dtmpickTo.Value.Date.AddDays(1)

In the query you should use the < operator to compare the end date. The
< operator is the exact complement to the >= operator, so that you will
get time ranges where no record will appear in two consecutive ranges

var query
from trans in dataSet.Transactio
where ((trans.T_Date >= qryStart
&& (trans.T_Date < qryEnd)) ...

--
G??ran Andersso
____
http://www.guffa.com

Re: Ignore Time element of DateTime?

I think you can chop the time off with a Tostring(format) and still do the
comparison.

var query =
from trans in dataSet.Transaction
where ((trans.T_Date.ToString("yyyyMMdd") >=
qryStart.Tostring("yyyMMdd")
&& (trans.T_Date.ToString("yyyyMMdd" <=
qryEnd.ToString('yyyyMMdd")) ....


I think there is a ToShortDate() which will chop off the time, and you
might be able to get away ToShortDate("yyyyMMdd") too.

Re: Ignore Time element of DateTime?
eGoran: thank you. That has solvd my problem

:

Re: Ignore Time element of DateTime?
Mr Arnold and Ken Foskey: thank you for your suggestions. I have actually
implemented Goran's suggestion but I was also wondering if it was possible to
'chop' off the Time element or at least set it to zero.

:


Submitted via EggHeadCafe - Software Developer Portal of Choice
WPF Binding Beyond the Limitation of Name Scopes
http://www.eggheadcafe.com/tutorial...f-49faac8854c8/wpf-binding-beyond-the-li.aspx
 
Please, foelks, don't do SILLY stuff like string conversions when all is needed it a little arithmetic...

static DateTime DatePart(DateTime dateTime)
{
return new DateTime(dateTime.Ticks - dateTime.Ticks % 864000000000L);
}

Cheers,
Axel

The question had to do with ignoring the date part of a dataset in SQL,
not a .Net DateTime.

More efficient to what you suggest would be to use the .Net property
that does what you coded: DateTime.Date. By the way, these both only
truncate the time part of the DateTime. The Time portion is still there
if you write out the object.
 
Back
Top