Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
any idea how to convert string="28/06/2016" to date with this format 2016-06-21?

in my c# i declare the date with string,my stored procedure is date format.

What I have tried:

DateTime.ParseExact(evendate, "yyyy-MM-dd", CultureInfo.InvariantCulture)
Posted
Updated 26-Jun-16 22:51pm
Comments
JayantaChatterjee 27-Jun-16 4:30am    
did you get any error on converting the date?
if you get errors then, what are the errors?

You should use date as date, when passing it between C# and SQL and not as string. Formatting it as string is only for display purposes...
But in case, you have the date already as string, because of some ignorant b..., do this:
C#
string evendate = "28/06/2016";
DateTime date = DateTime.ParseExact(evendate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
date.ToString("yyyy-MM-dd");
 
Share this answer
 
Well I don't know why you want to convert June 28, 2016 to June 21, 2016 and also I am not sure if that is a good thing to do or not? Anyways, you can play around with the following example. The last statement is probably what you need.

C#
DateTime myDate = DateTime.ParseExact("28-06-2016", "dd-MM-yyyy",
                                      System.Globalization.CultureInfo.InvariantCulture);
           Console.WriteLine(myDate);
           Console.WriteLine(myDate.ToString("MM-dd-yyyy"));
           Console.WriteLine(myDate.ToString("yyyy-dd-MM"));
           Console.WriteLine(myDate.ToString("yyyy-MM-dd"));


Hope it helps.
 
Share this answer
 
Comments
KyLim0211 27-Jun-16 4:47am    
not working
phil.o 27-Jun-16 5:03am    
"Not working" is not a valid issue description.
Simple:
C#
DateTime dt = DateTime.ParseExact(evendate, "dd/MM/yyyy", CultureInfo.InvariantCulture)

The "format" you supply to ParseExact or TryParseExact is the format of the input string, not any eventual presentation format you might apply. DateTime values don't have "a format" - they are stored as a number of ticks since a predefined point in time.
To format it for display, you have to convert it to a string. For your required format:
C#
string formatted = dt.ToString("yyyy-MM-dd");
Will do it

[edit]Typo[/edit]
 
Share this answer
 
v3
Comments
JayantaChatterjee 27-Jun-16 4:48am    
"st.ToString("yyyy-MM-dd");" would be "dt.ToString("yyyy-MM-dd");"... :-)
otherwise its simple and clear..
KyLim0211 27-Jun-16 4:52am    
not working
OriginalGriff 27-Jun-16 5:02am    
"Not working" is one of the most useless error reports we get: it tells us nothing.
Any message? Run time? Compilation? When does it occur? What happens that you didn't expect, or doesn't happen that you did?
KyLim0211 27-Jun-16 5:03am    
refer my new comment below thx
OriginalGriff 27-Jun-16 5:15am    
1) Don't post code as a solution - it's not an answer and it annoys people.
2) That's a totally different problem from the one you originally asked.
3) Use the code I gave you above to parse the text string into a DateTime value and pass that directly to SQL instead of the string. That'll fix it. Better still, use TryParse or TryParseExact and check that the user entered a valid date first - report an error if he didn't instead of continuing.
But I'd recommend not using a textbox for dates - use a Calendar or similar instead so that the user can't enter invalid data.

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