question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSON doesn’t have a date format so dates in graphene are just string fields. This means any dates that are output without being formatted first (e.g. if you use a DjangoNode with only_fields where one of the fields is a date field) are just output as unicode(date_object) (which looks like 2016-03-29 17:42:01.683650+00:00). If you take that date in JavaScript and do new Date(myDateStr) browsers will probably work it out but it’s not an officially supported date format (as we found out when we tried to parse it in React Native - i.e. Javascript Core on iOS - which doesn’t handle it).

I’m happy to create a pull request but I’m not sure what the best solution would be. Here are some options:

  • Make all dates output as 2016-10-01T14:12:00+01:00 which is a format JavaScript understands (which makes sense since we’re outputting JSON)
    • if we go down this route I’m not sure where the best place to hook it in would be - maybe another special case in coerce_string in graphql-core like there is for booleans?
    • another option would be to just do it for Django fields - make a DateTimeString type with a separate serialize function and modify convert_django_field for the DateField
  • Do one of the above but make it configurable somehow
  • If those are’t options (e.g. for backwards compatibility) then it would be nice to be able to hook in and do it ourselves without having to think about a fix every time we encounter a date field

Happy to do the work. If you’d like that, let me know which option you’d prefer and I’ll open a PR.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
rok-povsiccommented, Aug 22, 2018

I’ve solved the AssertionError: Received not compatible datetime "'2018-01-20 09:35:53+00:00'" issue by creating and using a custom type that overrides serialize and parses the string datetime:

from dateutil.parser import parse
class CustomGrapheneDateTime(graphene.DateTime):
    @staticmethod
    def serialize(date):
        if isinstance(date, str):
            date = parse(date)
        return graphene.DateTime.serialize(date)
4reactions
syrusakbarycommented, Apr 3, 2016

I finally added complete support for conversion from DateField to a DateTime scalar in graphene that will output the python date in a ISO-8601 format (Javascript Date format).

You can update graphene to the last version 0.8.0 and it will be working as expected.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Date Format Types
Date Format Types ; I. YY/day. Year-Day of Year (counting consecutively from January 1—often called the Julian date format) (2009/48) ; J ·...
Read more >
Date Format in the United States - ISO | MIT
The United States is one of the few countries that use “mm-dd-yyyy” as their date format–which is very very unique! The day is...
Read more >
Date format by country - Wikipedia
Writers have traditionally written abbreviated dates according to their local custom, creating all-numeric equivalents to day–month formats such as "20 December ...
Read more >
Date and Time Formats - IBM
Format Specifier Format Example %m/%d/%Y mm/dd/yyyy 02/21/2018 %m/%d/%y mm/dd/yy 02/21/18 %d/%m/%Y dd/mm/yyyy 21/02/2018
Read more >
Format a date the way you want - Microsoft Support
In the Format Cells box, click the Number tab. In the Category list, click Date. Under Type, pick a date format. Your format...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found