İçereği Atla
Menü
Bu soru işaretlendi
1 Cevapla
13246 Görünümler

in my custom model I defined the fields

time_from = fields.Datetime(string="Time From", default=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

time_to = fields.Datetime(string="Time To", default=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

tot_time = fields.Char("Time Difference", compute='_get_time')

this is my compute function

@api.depends('time_from', 'time_to')
def _get_time(self):
    t1 = datetime.datetime.strptime(self.time_from, '%Y-%m-%d %H:%M:%S')
    t2 = datetime.datetime.strptime(self.time_to, '%Y-%m-%d %H:%M:%S')
    if t2<t1:
        raise ValidationError('Time To must greater than Time From')
    time_diff = (t2-t1)
    self.tot_time = time_diff

This is success fully prints time difference. Time from and time to are mm/dd/yyyy hh:mm:ss format.

How to change this to mm/dd/yyyy hh:mm:ss format

I changed the format like this '%Y-%d-%m %H:%M:%S' .
but it is not getting correct result.
How to change the format?
is it possible to calculate time difference in this format?

Avatar
Vazgeç
En İyi Yanıt

The common syntax to change the date time format :

datetime.datetime.strptime(date_string, format1).strftime(format2)
Avatar
Vazgeç
İlgili Gönderiler Cevaplar Görünümler Aktivite
5
Eki 24
12678
2
Mar 24
2522
1
Oca 21
8744
2
Ağu 19
23886
4
Oca 20
6251