-9

I have a MySQL DateTime column that I need to query using C#. The problem is that when I try to query the column with the DateTime format of 'dd-MM-yyyy' it doesn't bring back any data but it does when I use the 'yyyy-MM-dd' format. I am wondering how I can convert my DateTime into that format?

Eg:

string yyyyMMdd = DateTime.Now.StartOfWeek(DayOfWeek.Monday).ToString("yyyy-MM-dd");

//Insert code to parse into yyyy-MM-dd DateTime format-

This gets the format I want in a string, but the column is a DateTime and every time I try to parse it, it returns back to the 'dd-MM-yyyy' format.

Man, stack overflow is so fickle. This question is different as I'm not asking how to format a DateTime into a different cultures format. I'm asking how to format a DateTime from the 'yy-MM-yyyy' to the 'yyyy-MM-dd' format.

Why the downvotes? Honestly.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Andrew Kilburn
  • 2,251
  • 6
  • 33
  • 65
  • 3
    You don't "parse a DateTime", you format it. Parsing is something you do to strings. The DateTime value does not have a format, the act of converting it to a string picks a format. If you want to have the DateTime value shown a specific way you need to take control over the formatting. Please clarify what the question really is. – Lasse V. Karlsen Jun 16 '16 at 08:13
  • You say, "I'm not asking how to format a DateTime", and then, "I'm asking how to format a DateTime from the 'yy-MM-yyyy' to the 'yyyy-MM-dd' format". It's really unclear what you're asking. – Enigmativity Jun 16 '16 at 08:17
  • 1
    You shouldn't need to do this at all, you should be using parameterised queries and just pass the `DateTime` as a parameter... – Trevor Pilley Jun 16 '16 at 08:18
  • @TrevorPilley Urgh, this is exactly what I'm doing in EF. – Andrew Kilburn Jun 16 '16 at 08:21
  • 1
    Your solution is wrong, dont waste your time to do this. Your question should be how to parse a datetime value to SQL. – Kason Jun 16 '16 at 08:21
  • @Enigmativity Okay, I'll edit it. I am asking how to format a DateTime but every post I've been pointed at doesn't answer my question. I know how to parse a DateTime, just not how to format it into a different culture – Andrew Kilburn Jun 16 '16 at 08:24
  • I'm lost. Since you're using EF anyway, shouldn't the code take DateTime as it is without issue? Can u post the exception (if any) and the complete code? – Martheen Jun 16 '16 at 08:40
  • 1
    @AndrewKilburn - There is no "different culture" for a `DateTime`. A `DateTime` is just a value type. It's only when you convert from a string or convert to a string that cultures get involved. The value of a `DateTime` is simply held in a `private UInt64 dateData;` field. – Enigmativity Jun 16 '16 at 08:47
  • 1
    @AndrewKilburn - You're getting down-votes because people are trying to help you, yet you respond with "stack overflow is so fickle" and "Urgh" and "Why the downvotes? Honestly." – Enigmativity Jun 16 '16 at 08:49

2 Answers2

0

DateTime.Parse() will try figure out the format of the given date, and it usually does a good job. If you can guarantee dates will always be in a given format then you can use ParseExact():

//string s = "2011-03-21";
string yyyyMMdd = DateTime.Now.StartOfWeek(DayOfWeek.Monday).ToString("yyyy-MM-dd");

DateTime dt = DateTime.ParseExact(yyyyMMdd , "yyyy-MM-dd", CultureInfo.InvariantCulture);

Source

Community
  • 1
  • 1
Igoranze
  • 1,506
  • 13
  • 35
-1

You could always try using:

DateTime testdate = DateTime.Now;

testdate.ToString("yyyy-MM-dd");

and just set the DateTime to whatever date and time you may need.