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.
44 Replies
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 .How can I change DateTime to Date
for simplicity you can compare
closeAccount.BirthDate.Date
instead of closeAccount.BirthDate
.lemme see Thank you
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
tThis approach is not working
Still it is taking same date format
This format is in my databse
c.BirthDate.Date == closeAccount.BirthDate.Date
Not yet
it is still null.
Can you show your Database model and DTO
Yes
yes so you're using
DateTime
as I said. You need to provide the exact DateTime value in your postman requestMeans.
exact DateTimemeans?
{
"AccountNumber": 4135120000000,
"BirthDate": "2024-11-11",
"CRN": 2200000000
}
This is what I am sending in Postman
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
Yes
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 endpointlike this?
[DataType(Date.Date)]
public Date BirthDate {get; set;}
in CloseAccountDTO
public Date BirthDate { get; set; }
Error throwing
Got it
one thing imported
lemme check onow
Again Error
This is newly imported package
using static System.Runtime.InteropServices.JavaScript.JSType;
no dont do that eh
don't import any such package
Without this package it is showing error
change it back to
DateTime
and perhaps send the dat in the postman request in the reverse order and then compare it like this :
So send the date in a format which it's expecting
why the heck is DTO getting it as 2024
thoughIt is not possible to send date in expected format. How user can send like this {11-11-2024 00:00:00}
normally users don't send it manually lol, you send it to API programmatically
isn't there any method to simplyfy this
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
Hey, It works
Thanks to you
if it works then dont touch it :catlaugh:
Rule of programming
wait
?
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.
I will go through it
also $close
If you have no further questions, please use /close to mark the forum thread as answered