3

This is my article model:

class Article(models.Model):
    url = models.CharField(max_length=256)
    headline = models.CharField(max_length=256)
    source = models.ForeignKey(Source)
    datetime_ingested = models.DateTimeField(auto_now_add=True)
    content = models.TextField()
    image_src = models.CharField(max_length=256, null=True)

And this is my serializer:

class ArticleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ('id', 'url', 'headline', 'source', 'datetime_ingested', 'content', 'image_src')

All date time fields are in UTC, and I want to convert the time to the client's localtime. Is this possible to do from the framework's side? Right now I'm serving the content, parsing the date using moment.js (https://momentjs.com/) to display contextual datetime based on the current timezone like so:

moment(article['datetime_ingested'], "YYYY-MM-DD hh:mm:ss").fromNow()

But this translates UTC to now. Is it possible to tell Django to serve the datetime fields in the client's timezone?

The datetime string looks like so:

2017-01-26T03:49:34.232000Z
Stupid.Fat.Cat
  • 10,755
  • 23
  • 83
  • 144
  • When parsing a date string, you should **always** provide the format. What does `article['datetime_ingested']` look like? – RobG Jan 26 '17 at 06:42
  • @RobG looks like this: 2017-01-26T03:49:34.232000Z, I believe Z is UTC. – Stupid.Fat.Cat Jan 26 '17 at 07:57
  • http header does not include client locale... so you have to JS to get the timezone, and keep the timezone in session/cookie... you can have a look of this post...http://stackoverflow.com/questions/3001260/how-to-detect-client-timezone – Enix Jan 26 '17 at 09:58

0 Answers0