How to maintain the datetime format same as input
See original GitHub issueFirst 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:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here is a simple example using
jsonable_encoder
of FastAPI:Output:
Actually The converting is executed by Pydantic, not by FastAPI.