-1

I have a date that is coming from the database like so:

26/01/2016 12:00:00 AM

and I am capturing it as a string

questionItem.DueDate = dataReader[6].ToString();

I am trying to reformat this string so its looks like

2016-01-26

How would I do this?

1
  • 4
    What is the actual type of the database column with the date? Commented Jan 26, 2016 at 18:44

3 Answers 3

6

You can specify format like below.

 questionItem.DueDate = Convert.ToDateTime(dataReader[6]).ToString("yyyy-MM-dd");

Check MSDN form DateTime formating.

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

Comments

3

Should look something like this

questionItem.DueDate = dataReader[6].ToString("yyyy-MM-dd");

For further readings checkout https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx

2 Comments

This gives me an error: Error 1 No overload for method 'ToString' takes 1 arguments
Could you provide the code you are having problems with?
0

Two options:

questionItem.DueDate = new DateTime(dataReader[6]).ToString("yyyy-MM-dd");

Or

questionItem.DueDate = dataReader[6].Split(new string[] { " " }, StringSplitOptions.None)[0].Replace('\\', '-');

Comments

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.