Datetime issues

Posts   
 
    
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 28-May-2005 13:18:28   

Hi,

I aplogize beforehand if i ask a n00b question, but i have allready spent quite some time on this one allready and i just don't see it cry

I have a field on my entity which is in the DB of type DateTime. It allways saves the value in the format "dd/mm/yyyy hh:mm:ss". If i use in my gui a datetimepicker to select a date, the time is set to the current time, and saved into the record as hh:mm:ss

What i want now is to be able to save without the time, only the date dd/mm/yyyy (do i have to override a method in the entitybase class for that ?)

And i want to be able to filter on my records, so the user can select a date in a datetimepicker, and then use this selected date into a predicate

something like :


                    string date = dtReceptieDatum_nt.Value.ToShortDateString();
                    PredicateExpression filter= new PredicateExpression();
                    filter.Add(PredicateFactory.Like(ReceptionPerceelFieldIndex.RecplZaaidatum, date));

It just doesn't work rage Anyone has tackled this before ?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 28-May-2005 13:50:50   

Use a between predicate: between date, 00:00:00 and date, 23:59:59

Frans Bouma | Lead developer LLBLGen Pro
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 28-May-2005 16:09:17   

Ok, i had thought of that too but i wanted to see if there was a simpeler solution :-)

i found a nice example in the thread http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=332 of exactly what i want :-) So i see you use the between operator in


    predicateToAdd = PredicateFactory.Between(_criteria[i].EntityFieldIndex, GetCriteriumValue(_criteria[i], true), GetCriteriumValue(_criteria[i], false));

Now i start understanding it ..i assume you have an overloaded "GetCriteriumValue" for datetimes, that takes a second argument bool that indicates whether the datetime is at 00:00:00 or 23:.59.59 , right ?

But how can i make sure that i persist dates in the DB in the format dd/mm/yyyy , without the time ?

i did a little test , replaced the first line with the second :

            //rp.RecplZaaidatum = dtZaaiDatum_nt.Value;
            rp.RecplZaaidatum = Convert.ToDateTime("28/05/2005");

and then when persisting, the time is cut off in the DB :

12  1   10  28/05/2005 5:08:42  <- the last record i entered last night, then i gave up :P  
13  1   6   28/05/2005  <- the one added just now

Any suggestion how i could do that using databinding to a datetimepicker ?

Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 28-May-2005 16:41:37   

HcD wrote:

            //rp.RecplZaaidatum = dtZaaiDatum_nt.Value;
            rp.RecplZaaidatum = Convert.ToDateTime("28/05/2005");

and then when persisting, the time is cut off in the DB :

12  1   10  28/05/2005 5:08:42  <- the last record i entered last night, then i gave up :P  
13  1   6   28/05/2005  <- the one added just now

Any suggestion how i could do that using databinding to a datetimepicker ?

If dtZaaiDatum_nt.Value is DateTime type, then: rp.RecplZaaidatum = dtZaaiDatum_nt.Value.Date;

HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 28-May-2005 17:23:04   

Yups, i found that out too, but it's impossible to use databingding and bind it to the "Value.Date" property of a DateTimePicker control, because that "Date" property is read-only rage

I guess i'll try to solve by iterating over all datetimes right before the persist, and "convert" them to date using "datetimefield = datetimefield.Date;"

Where in the enitybase class would be the best spot to insert that interception code ?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 28-May-2005 17:40:59   

Aren't you using Oracle? If so, Oracle has a 'Date' type.

Dates selected from a datetime picker have the time set to 0.0.0 if I'm not mistaken.

Frans Bouma | Lead developer LLBLGen Pro
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 28-May-2005 17:55:55   

hah, i had what they call a "fijne vondst" and i quicksolved it using the following property in the user region code (so i don't have to fiddle in the BaseEntity class)


        //override theEntityBase property
        public override System.DateTime RecplZaaidatum
        {
            get
            {return base.RecplZaaidatum.Date;}
            set
            {base.RecplZaaidatum = value.Date;}
        }


Now i can just bind RecPLZaaidatum to the Value of a Datetimepickercontrol, and still only have the Date ... works like a charm smile

edit: nopes, using SQL2000 :-)

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 28-May-2005 18:14:59   

aha simple_smile Sounds indeed like a 'goede ingeving' wink

Frans Bouma | Lead developer LLBLGen Pro
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 28-May-2005 18:34:47   

oh, and added bonus is that


DateTime datefilter = dtpickerReceptiePerceelZaaidatum_nt.Value.Date;
pe.Add(PredicateFactory.CompareValue(ReceptionPerceelFieldIndex.RecPlZaaidatum, ComparisonOperator.Equal, datefilter));

works now simple_smile No more need for an "between" operator (probably more performant too ?)

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 28-May-2005 18:46:47   

Well, keep in mind that the time is set to 0.0.0 in those dates for the data saved through the entity. When some 3rd party code / bulk inserter, whatever, inserts rows too it might be different.

Frans Bouma | Lead developer LLBLGen Pro