Skip to content

Instantly share code, notes, and snippets.

@hwshim0810 hwshim0810/dates.py
Created Jul 6, 2018

Embed
What would you like to do?
Restframework custom timestamp field
from django.utils import timezone
from rest_framework import serializers
class DateToTimestampField(serializers.Field):
def to_representation(self, value):
epoch = timezone.datetime(1970, 1, 1)
value = timezone.datetime.fromordinal(value.toordinal())
return int((value - epoch).total_seconds()) * 1000
class DatetimeToTimestampField(serializers.Field):
def to_representation(self, value):
epoch = timezone.datetime(1970, 1, 1)
return int((value - epoch).total_seconds()) * 1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.