简体   繁体   中英

Parsing Django DateTimeField in Javascript(React)

I built my backend api with django rest framework in which the model has a DateTimeField.

date = models.DateTimeField(auto_now_add=True)

When I get the data from the api to React I get something like this

2019-01-20T19:24:58.674435Z

How can I parse this in javascript and convert to a readable form?

It's possible to do it through your serializer. Given that you name it ''date'' in your model, you can do the following.

class ObjectSerializer(serializers.ModelSerializer):

date = serializers.DateTimeField(format="%d-%m-%Y %H:%M:%S")


class Meta:
    model = Object
    fields='__all__'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM