Skip to content

Instantly share code, notes, and snippets.

@hwshim0810
Created July 6, 2018 02:33
Show Gist options
  • Save hwshim0810/ed1ca013a4ca62633d266814ba50070c to your computer and use it in GitHub Desktop.
Save hwshim0810/ed1ca013a4ca62633d266814ba50070c to your computer and use it in GitHub Desktop.
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