Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

I have the code like:
(C#,SQL)

DateTime tempdate = DateTime.Now;
string dt = tempdate.ToString("yyyy-MM-dd HH:mm:ss.fff");

[now dt have the value like:2014-09-05 10:52:30.000]

drDomain.CREATED_DATE = Convert.ToDateTime(dt);

//here "CREATED_DATE" is a datetime varaible, again its converting into 05/09/2014 10:52:30 a.m format, but I need to keep value in "2014-09-05 10:52:30.000" format. but it was in string varaible. How to keep the value in the "yyyy-MM-dd HH:mm:ss.fff" format and convert this to datetime varaible type:

Note: I am not using stored procedures;

thanks in Advance

What I have tried:

DateTime tempdate = DateTime.Now;
string dt = tempdate.ToString("yyyy-MM-dd HH:mm:ss.fff");

[now dt have the value like:2014-09-05 10:52:30.000]

drDomain.CREATED_DATE = Convert.ToDateTime(dt);
Posted
Comments
Tomas Takac 15-Feb-16 3:05am    
Not clear. You are just converting the value back and forth between datetime and string. You mentioned you are using stored procedures yet the code you are showing is C#. You need to explain where is the problem.
Wombaticus 15-Feb-16 3:10am    
I don't understand...
tempdate = Now
dt = Now, formatted nicely as a string
drDomain.CREATED_DATE = dt ( = Now) converted back to a date
WHat is the point of all that?

If you want to format the time Now for SQL then why not
string dt = Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
though persoanlly I prefer
string dt = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}", Now)
Member 10714689 15-Feb-16 4:02am    
hi,
as per your suggestion i will take, string dt=String.Foramt(:{0:yyyy-MM-dd HH:mm:ss.ff}",Now);
Now I have to pass "dt"value to "Created_date" Variable which is DATETIME type. how to pass dt (string) value without changing its format to DATETIME varaible.

Thanks...
Wombaticus 15-Feb-16 4:09am    
Your question doesn't make sense.... a DATETIME variable is not a string.

1 solution

Don't.
DateTime objects - in C# and SQL - are just that, they specify a "point in time" and are stored as a number of milliseconds since a defined date and time. What they don't include is any formatting information, timezone info, or anything else other than "n milliseconds since the base moment".

Everything else is a presentation function, and the only way to preserve that information "for later" is to store it as a string - which is a very, very poor idea as it both prevents you from doing any simple math or comparisons with it, and prevents you showing it in the user's preferred format.

Always store it as a DateTime, and convert it to a "presentable" date as late as possible.
 
Share this answer
 

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