Click here to Skip to main content
15,910,234 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am passing a date from C# and comparing the same with a date field from the database to retrieve a record but not able to do so since the date format is not matching.
The date coming from C# is like '11/24/2011 10:59:50 AM' while the date in the database is '2011-11-24 10:59:50.360'
How do I convert the C# date to the format of the Database?
Posted

C#
DateTime a = GetFirstDate();
DateTime b = GetSecondDate(); // date from SQL

if (a.Date.Equals(b.Date))
{
    // the dates are equal
}
 
Share this answer
 
v2
Comments
RaviRanjanKr 25-Nov-11 9:04am    
My 5+
Covert date from c# to this format:

string date = DateTime.Now.ToString("mm-dd-yyyy hh:MM:ss.fff");

Then compare.

Regards.
 
Share this answer
 
Comments
gpthakur 25-Nov-11 3:56am    
I am able to convert the datetime but I am missing milliseconds in the format due to which I am not able to match the records in the DB.
What I am getting is 2011-11-24 10:59:50:000' but what I need is 2011-11-24 10:59:50:360
[no name] 25-Nov-11 4:08am    
i'll add a new solution, check it out. im pretty sure this one will work.
You can change the format of the C# Date as below

DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff")

This gives you the similar output as DataBase Date format is.
 
Share this answer
 
Comments
gpthakur 25-Nov-11 1:30am    
Hi Rajeev,

I have a field as LastUpdate which I am appending to my query as below. How do I then convert it?

Select UA.Reaction, UAD.OnsetDate, LastEpisodeDate, TreatmentMethod,ClinicianName,UA.Comments from UserAllergy UA Inner Join UserAllergyDetail UAD on UA.LastUpdate = UAD.LastUpdate where UA.LastUpdate ='" + LastUpdate + "'"
Hi again gpthakur,

Hope this one works! try this one instead:

C#
string date = DateTime.Now.ToString("mm-dd-yyyy");
string time = DateTime.Now.ToString("hh:MM:ss.fff");

string result = date + " " + time;


Please mark this solution as solved it fixes your problem.

Best Regards,
Eduard
 
Share this answer
 
Comments
gpthakur 25-Nov-11 4:37am    
Eduard, this is not working :(
Have a look at the below code.

Select UA.Reaction, UAD.OnsetDate, LastEpisodeDate, TreatmentMethod,ClinicianName,UA.Comments from UserAllergy UA Inner Join UserAllergyDetail
UAD on UA.LastUpdate = UAD.LastUpdate where UA.LastUpdate ='" + LastUpdate.ToString(dtformat)

This is my query through which I am reading the field LastUpdate.
How do I format this field to get the desired output is my problem.
Currently what I am using is LastUpdate.ToString(dtformat) where dtformat is "yyyy-MM-dd HH:mm:ss.fff"
[no name] 25-Nov-11 4:45am    
Correct me if im wrong:
OUPUTS:
C# = '2011-11-24 10:59:50 AM'
SQL = '2011-11-24 10:59:50.360'

are these the values you are getting currently?
there's no need to convert the data you get from sql. just convert the date in c# as to what the sql date format is.
gpthakur 25-Nov-11 5:10am    
Yes, what you values u have mentioned here is correct and I am not converting SQL values. But when I am trying to convert the C# format to SQL, I am losing the milliseconds and hence I am not getting a perfect match for database records. The problem in on line where I am specifying the format for date time i.e yyyy-MM-dd HH:mm:ss.fff
[no name] 25-Nov-11 5:20am    
i cant see the problem. Im getting the correct format using the above code.

C# = '2011-11-24 10:59:50.360'
gpthakur 25-Nov-11 5:25am    
Eduard, when I run through the SQL Profile, I get the below query which trims the milliseconds

Select UA.Reaction, UAD.OnsetDate, LastEpisodeDate, TreatmentMethod,ClinicianName,UA.Comments from UserAllergy UA Inner Join UserAllergyDetail
UAD on UA.LastUpdate = UAD.LastUpdate where UA.LastUpdate ='2011-11-24 10:59:50.000'
Tks for your help anyways :)
set c# date with this format ,

string date = DateTime.Now.ToString("mm-dd-yyyy hh:MM:ss.fff");

then compare..


hope it helps!
 
Share this answer
 
Comments
[no name] 25-Nov-11 3:43am    
Hey you, you stole my comment!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900