Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Frnds,

any one knows how to convert date time from (27/10/2011) to (2010/10/27)


i need to insert in to database table(sql format (yyyy/mm/dd).



please any one knows well in this scenario help me

Thaks
Venkat.S
Posted
Comments
Al Moje 27-Oct-11 4:13am    
Hi,
Google is your friend. There were so many kinds of date format….
Search key should be ‘how sql format (yyyy/mm/dd)’

A date can be converted to any format string with the ToString("format") overload for example date.ToString("yyyy-MM-dd").

Use '-' as a separator instead of '/' for dates as some database engines have problems with '/'.
 
Share this answer
 
Comments
Anil Honey 206 27-Oct-11 4:26am    
Excuseme sir,
How to Insert in this format in c# 27 Oct 2011 1:59 AM
If you want to do this in C#.Net, use below syntax.
C#
string myDate = DateTime.Now.ToString("yyyy/MM/dd");

In case you want to do this in SQL, then have a look at below link.
http://www.w3schools.com/sql/func_convert.asp
 
Share this answer
 
The first this to do is: do not convert the date to any string format - instead, convert it to a DateTime value and store that directly in the database (in a SQL DateTime field). You can pass it in as a parameter, so it doesn't need to be converted.

It is easy to convert a string to a datetime:
C#
DateTime dt = DateTime.Parse(dateAsString);
This takes into account whatever date format settings the user has applied on his PC, so you don't have to concern yourself. It also has the advantage that any invalid dates are caught at this point, before they enter your database to cause problems later.

Storing it as a DateTime in the Database means that it is easier to use in comparisons, and also to format to the user PC settings when you retrieve the info later.
 
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