Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django datetime to unix timestamp

my model

create = models.DateTimeField(auto_now_add=True)

how to get unix time from this field?

datetime.datetime.timestamp()

but I need int

like image 487
Aleksandr Vaskevich Avatar asked Mar 10 '23 17:03

Aleksandr Vaskevich


1 Answers

Here create is a simple python datetime.datetime instance itself.

For python less than 2.x

You can do .

my_model = MyModel()
int(my_model.create.strftime('%s'))

For python 3.3+

You can just do

my_model = MyModel()
my_model.create.timestamp()
like image 178
Bipul Jain Avatar answered Mar 16 '23 20:03

Bipul Jain