Help with default datetime field

Hi,

In a custom model, i’m using a datetime field for calendar event.
How to set a default planned date to have today as date and ‘00:00:00’ as time ?

I try such code:

@classmethod                                                                                     
def default_planned_date(cls):                                                                                                                  
    return dt.datetime.now().replace(hour=0, minute=0, second=0,                                 
        microsecond=0)

But I have ‘01:00:00’ instead of ‘00:00:00’ as time

When returning datetime.now()

@classmethod                                                                                     
def default_planned_date(cls):                                                                                                                  
    return dt.datetime.now()

Then time is correct…

Any idea ?

This is because on server side, the datetime are always in UTC but on the client side the datetime is rendered in the timezone of the OS.

So I do not know what you want to represent but you should consider:

  • is it really a fixed point in time, so you must use a datetime but set time part to 0 has no real meaning
  • is it a conventional time of a date, so it is probably a date and a time fields.

Thanks for the explanation.

We have 2 types of shipment events:

  • We don’t know what time it should be delivered/shipped, so we don’t care about time to have an “all day event” (ics calendar event with only a dtstart without time)
  • The carrier warns us about the delivery/shipping time so we need the time to have the event at the correct time in the calendar (there’s a function field that define an “end” with a 1 hour interval from specified time) (ics calendar event with dtstart and dtend)

Probably, I should have two distinct fields : date and time. Time field will be invisible in case of an “All day event” (boolean field)

Anyway, why is the time “now” with the correct timezone and the “forced” time to midnight with 1 hour more ?

Because now is now in every timezones.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.