Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to convert 2013/12/03 into 20131203

I am using calender control in my C# code and i need to save 'date' format into 'int' in my DB.

If anyone can give me some answers on how to do will be very great full.

Thanks,
Posted

It is a best practice to store dates as DATE data type in the database. By storing the date as an integer, you complicate all processing that must be done with the date and increase the chance of programming errors. Please reconsider using INT data type for your date. The DATE data type takes no additional space and provides ease of use and protection from common logic errors.
 
Share this answer
 
Comments
Thomas Daniels 2-Jan-14 8:20am    
+5!
C#
string datestr = "2013/12/03";
       // simple way is
       int dateInt = Convert.ToInt32(datestr.Replace("/", ""));

       // ( or )
       // using date object
       DateTime dt = DateTime.ParseExact(datestr, "yyyy/MM/dd", System.Globalization.CultureInfo.InvariantCulture);
       int dateInt2 = Convert.ToInt32(dt.ToString("yyyyMMdd"));
 
Share this answer
 
Use DateTime's Ticks property - its better than 20131203 format as it can be used to reproduce the date without knowledge of how to formatting was done.
 
Share this answer
 
Here is how you would get the date as of now into an integer.
I will let you work out the rest, as I don't know the names of variables etc. in your code:

C#
int iso = new int();
int.TryParse(DateTime.Now.ToString("yyyyMMdd"), out iso);
 
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