convert Date to milliseconds

82 views
Skip to first unread message

HJ

unread,
May 25, 2020, 6:17:19 PM5/25/20
to Django users
Hello everyone I want to convert my Date from string to milliseconds

this is my models.py class 

class mail_item_count_deliveries_perDate(models.Model):
   countDeliveries = models.IntegerField(default=0)
   Date = models.DateTimeField()

this is my views.py 

def jsonDeliv(request):
 
 dataset = mail_item_count_deliveries_perDate.objects.all().values_list('Date','countDeliveries')
 data = list(dataset)
 
 return JsonResponse(data, safe=False)

this is how the data looks like 

[["2020-05-21T00:00:00Z", 5], ["2019-05-21T00:00:00Z", 1], ["2020-04-06T00:00:00Z", 3], ["2020-02-10T00:00:00Z", 2], ["2020-01-23T00:00:00Z", 4],

I want to convert the Date to milliseconds I want to use it in stock highchart , and I don't know how to do it here can you guys help me please 

Samuel Nogueira

unread,
May 25, 2020, 8:01:17 PM5/25/20
to django...@googlegroups.com

Hi! before answer you question I would recomend you to give a little fix in your model and your function names. Try to follow the PEP8 style guide for Python, in the guide is recommended using only capitalized words for naming classes with composite names, and function names should be lowercase with words separated by underscore. Thereby, would be more appropriate naming your function and class this way: class MailItemCountDeliveriesPerDate() and def json_deliv(). Fell free to accept or ignore this tip.

Now, back to the question, the DateTimeField() is a implementation of the python datetime.datetime for storage in the database, so after you get the Date from the database you can use the timestamp() method in the related Date field. This will deliver the date in seconds format, then you just multiply by 1000 to get your Date in milliseconds.

 

Hope this helps. Sorry if it got a little confusing.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/de8d3e67-2a68-45be-a9c9-fb06fbb206a6%40googlegroups.com.

 

HJ

unread,
May 25, 2020, 8:22:12 PM5/25/20
to Django users
thank you so much for your ideas , I am gonna fix this ^^

To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages