Date not getting in required format.

I want date like this 2000-11-11, I am sending like this in postman, But API taking it like this {11-11-2024 00:00:00}. How can I fix this. I am compairnig date in database and date coming through client.
No description
No description
44 Replies
Salman
Salman2mo ago
2000-11-11 is Date but 11-11-2024 00:00:00 is DateTime two different types. You either need to change the DateTime to Date in your api or provide the full DateTime value in the postman .
Serene Sphinx
Serene SphinxOP2mo ago
How can I change DateTime to Date
Salman
Salman2mo ago
for simplicity you can compare closeAccount.BirthDate.Date instead of closeAccount.BirthDate.
Serene Sphinx
Serene SphinxOP2mo ago
lemme see Thank you
Salman
Salman2mo ago
to change it completely you would need to change your DB Model and DTO etc if you can afford that but I think the above approach should work for now
Serene Sphinx
Serene SphinxOP2mo ago
tThis approach is not working
Serene Sphinx
Serene SphinxOP2mo ago
No description
Serene Sphinx
Serene SphinxOP2mo ago
Still it is taking same date format
Serene Sphinx
Serene SphinxOP2mo ago
This format is in my databse
No description
Salman
Salman2mo ago
c.BirthDate.Date == closeAccount.BirthDate.Date
Serene Sphinx
Serene SphinxOP2mo ago
Not yet
No description
Serene Sphinx
Serene SphinxOP2mo ago
it is still null.
Salman
Salman2mo ago
Can you show your Database model and DTO
Serene Sphinx
Serene SphinxOP2mo ago
Yes
Serene Sphinx
Serene SphinxOP2mo ago
No description
Serene Sphinx
Serene SphinxOP2mo ago
No description
Salman
Salman2mo ago
yes so you're using DateTime as I said. You need to provide the exact DateTime value in your postman request
Serene Sphinx
Serene SphinxOP2mo ago
Means. exact DateTimemeans? { "AccountNumber": 4135120000000, "BirthDate": "2024-11-11", "CRN": 2200000000 } This is what I am sending in Postman
Salman
Salman2mo ago
that's only Date and have no time in it. Can you show the complete screenshot of your database record ? also which database are you using
Serene Sphinx
Serene SphinxOP2mo ago
Yes
Serene Sphinx
Serene SphinxOP2mo ago
No description
Salman
Salman2mo ago
hm that's weird. So perhaps try the datatype to Date in your DTO and then see only in DTO then save, send the request with postman and debug the coming value in the endpoint
Serene Sphinx
Serene SphinxOP2mo ago
like this? [DataType(Date.Date)]
Salman
Salman2mo ago
public Date BirthDate {get; set;} in CloseAccountDTO
Serene Sphinx
Serene SphinxOP2mo ago
public Date BirthDate { get; set; } Error throwing Got it one thing imported lemme check onow
Serene Sphinx
Serene SphinxOP2mo ago
No description
Serene Sphinx
Serene SphinxOP2mo ago
Again Error This is newly imported package using static System.Runtime.InteropServices.JavaScript.JSType;
Salman
Salman2mo ago
no dont do that eh don't import any such package
Serene Sphinx
Serene SphinxOP2mo ago
Without this package it is showing error
Serene Sphinx
Serene SphinxOP2mo ago
No description
Salman
Salman2mo ago
change it back to DateTime and perhaps send the dat in the postman request in the reverse order and then compare it like this :
c.BirthDate == closeAccount.BirthDate.Date
c.BirthDate == closeAccount.BirthDate.Date
So send the date in a format which it's expecting why the heck is DTO getting it as 2024 though
Serene Sphinx
Serene SphinxOP2mo ago
It is not possible to send date in expected format. How user can send like this {11-11-2024 00:00:00}
Salman
Salman2mo ago
normally users don't send it manually lol, you send it to API programmatically
Serene Sphinx
Serene SphinxOP2mo ago
isn't there any method to simplyfy this
Salman
Salman2mo ago
first try if it works by sending the date in the reverse format that dotnet is expecting and then compare it like I showed above
Serene Sphinx
Serene SphinxOP2mo ago
Hey, It works Thanks to you
Salman
Salman2mo ago
if it works then dont touch it :catlaugh:
Serene Sphinx
Serene SphinxOP2mo ago
Rule of programming
Salman
Salman2mo ago
wait
Serene Sphinx
Serene SphinxOP2mo ago
?
Salman
Salman2mo ago
Go through this article, it shows how to customize the default date time structure in c# . It's very well explained : https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
Custom date and time format strings - .NET
Learn to use custom date and time format strings to convert DateTime or DateTimeOffset values into text representations, or to parse strings for dates & times.
Serene Sphinx
Serene SphinxOP2mo ago
I will go through it
Salman
Salman2mo ago
also $close
MODiX
MODiX2mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server