1

I am reading excel file in my C# .net web application and storing that value in Database. Ony of field in Excel is DateTime. I am storing it in string

string tran_time = Convert.ToString(odr[5]); //tran_time is "03-11-2012 16:08:43"

and then convert it in in DateTime and store it in Database (SQL Server 2008)

IFormatProvider culture = new CultureInfo("en-US", true);
DateTime dateVal = DateTime.ParseExact(tran_time, "dd-MM-yyyy HH:mm:ss", culture);

But the Value Being stored in Database is in format

2012-05-11 13:40:23.000 (yyyy-mm-dd)

and the value in Excel is 05-11-2012 13:40:23 (dd-mm-yyyy)

Date & Month is get replaced.

My Question is How can i store it in Database in Format (YYYY-MM-DD HH:MM:SS:FFF)

2
  • 2
    "But the Value Being stored in Database is in format [...]" - why aren't you storing it as a DateTime in the database? Then you don't have a format. Commented Nov 5, 2012 at 15:54
  • In Database that Column is of type "DateTime" Commented Nov 5, 2012 at 15:56

3 Answers 3

3

But the Value Being stored in Database is in format

No it isn't (assuming that you are using a DATETIME column type). This is just what the SQL tools show you.

A DateTime instance, either in a database or in C# does not have an associated format. It only gets formatted when displayed to the user.

Sign up to request clarification or add additional context in comments.

Comments

0

Or use yyyy-MM-dd format. Works like a charm:

string myString = dateVal.ToString("yyyy-MM-dd");

Comments

0

Use dd-mm-yyyy instead of dd-MM-yyyy.

1 Comment

Then your input string does not contain the months in numeric format? Check out the MSDN documentation at msdn.microsoft.com/en-us/library/az4se3k1.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.