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.

How to maintain the datetime format same as input

See original GitHub issue

First check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.
  • After submitting this, I commit to one of:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • I already hit the “watch” button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug.

I have created a model as below.

class Inventory(BaseModel): locationId: str = Field(…, example=“LOC44242”, description=“Unique Store/Location identifier.”) inventoryDate: datetime = Field(…, example=“2019-04-01T00:00:00.000Z”, description=“ISO 8601 format”)

I am sending inventoryDate in 2019-04-01T00:00:00.000Z format I want FastAPI to maintain this format but it is changing it to 2019-05-01T11:53:21.988000+00:00

is there a way yo maintain the format?

Example

Here’s a self-contained, minimal, reproducible, example with my use case:

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}

Description

  • Open the browser and call the endpoint /.
  • It returns a JSON with {"Hello": "World"}.
  • But I expected it to return {"Hello": "Sara"}.

Environment

  • OS: [e.g. Linux / Windows / macOS]:
  • FastAPI Version [e.g. 0.3.0]:

To know the FastAPI version use:

python -c "import fastapi; print(fastapi.__version__)"
  • Python version:

To know the Python version use:

python --version

Additional context

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
waynervcommented, May 7, 2021

Here is a simple example using jsonable_encoder of FastAPI:

from datetime import datetime

from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel, Field


class Inventory(BaseModel):
    locationId: str = Field(..., example="LOC44242", description="Unique Store/Location identifier.")
    inventoryDate: datetime = Field(..., example="2019-04-01T00:00:00.000Z", description="ISO 8601 format")


a = Inventory(locationId='LOC44242', inventoryDate='2019-04-01T11:53:21.980Z')

print(jsonable_encoder(a.dict(), custom_encoder={datetime: lambda dt: dt.isoformat().replace("+00:00", "Z")}))

Output:

{'locationId': 'LOC44242', 'inventoryDate': '2019-04-01T11:53:21.980000Z'}
1reaction
waynervcommented, May 6, 2021

Actually The converting is executed by Pydantic, not by FastAPI.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to keep date format the same in pandas? - Stack Overflow
Then your best bet is to not parse the dates in the first place, as that will preserve the string format. You can...
Read more >
<input type="datetime-local"> - HTML - MDN Web Docs
A string representing the value of the date entered into the input. The format of the date and time value used by this...
Read more >
Custom date and time format strings | Microsoft Learn
In parsing operations, custom date and time format strings can be used with the DateTime.ParseExact, DateTime.TryParseExact, DateTimeOffset.
Read more >
Formatting and parsing dateTimes as strings - IBM
Characters for formatting a dateTime as a string ; HH, hour of day in 24 hour form (00-23) · Number ; I, ISO8601...
Read more >
Demystifying DateTime Manipulation in JavaScript - Toptal
You can use a different combination of letters preceded by % to get the date in different formats. (Careful, not every language assigns...
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