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
Salman2w 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 SphinxOP2w ago
How can I change DateTime to Date
Salman
Salman2w ago
for simplicity you can compare closeAccount.BirthDate.Date instead of closeAccount.BirthDate.
Serene Sphinx
Serene SphinxOP2w ago
lemme see Thank you
Salman
Salman2w 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 SphinxOP2w ago
tThis approach is not working
Serene Sphinx
Serene SphinxOP2w ago
No description
Serene Sphinx
Serene SphinxOP2w ago
Still it is taking same date format
Serene Sphinx
Serene SphinxOP2w ago
This format is in my databse
No description
Salman
Salman2w ago
c.BirthDate.Date == closeAccount.BirthDate.Date
Serene Sphinx
Serene SphinxOP2w ago
Not yet
No description
Serene Sphinx
Serene SphinxOP2w ago
it is still null.
Salman
Salman2w ago
Can you show your Database model and DTO
Serene Sphinx
Serene SphinxOP2w ago
Yes
Serene Sphinx
Serene SphinxOP2w ago
No description
Serene Sphinx
Serene SphinxOP2w ago
No description
Salman
Salman2w 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 SphinxOP2w ago
Means. exact DateTimemeans? { "AccountNumber": 4135120000000, "BirthDate": "2024-11-11", "CRN": 2200000000 } This is what I am sending in Postman
Salman
Salman2w 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 SphinxOP2w ago
Yes
Serene Sphinx
Serene SphinxOP2w ago
No description
Salman
Salman2w 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 SphinxOP2w ago
like this? [DataType(Date.Date)]
Salman
Salman2w ago
public Date BirthDate {get; set;} in CloseAccountDTO
Serene Sphinx
Serene SphinxOP2w ago
public Date BirthDate { get; set; } Error throwing Got it one thing imported lemme check onow
Serene Sphinx
Serene SphinxOP2w ago
No description
Serene Sphinx
Serene SphinxOP2w ago
Again Error This is newly imported package using static System.Runtime.InteropServices.JavaScript.JSType;
Salman
Salman2w ago
no dont do that eh don't import any such package
Serene Sphinx
Serene SphinxOP2w ago
Without this package it is showing error
Serene Sphinx
Serene SphinxOP2w ago
No description
Salman
Salman2w 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 SphinxOP2w ago
It is not possible to send date in expected format. How user can send like this {11-11-2024 00:00:00}
Salman
Salman2w ago
normally users don't send it manually lol, you send it to API programmatically
Serene Sphinx
Serene SphinxOP2w ago
isn't there any method to simplyfy this
Salman
Salman2w 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 SphinxOP2w ago
Hey, It works Thanks to you
Salman
Salman2w ago
if it works then dont touch it :catlaugh:
Serene Sphinx
Serene SphinxOP2w ago
Rule of programming
Salman
Salman2w ago
wait
Serene Sphinx
Serene SphinxOP2w ago
?
Salman
Salman2w 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 SphinxOP2w ago
I will go through it
Salman
Salman2w ago
also $close
MODiX
MODiX2w 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