0

Currently my function outputs date as 6/6/2016 and I need to be able to make it as 06/06/2016 for all the data. I found DateTime.Now.ToString("dd") is a solution. But when I do parts[0].DateTime.Now.ToString("dd") it doesn't accept it.

private static string FormatDate(string sDate)
{
    // "6/16/1989"
    //mysql format 06-16-1989

    string[] parts = sDate.Split('/');

    //sDay.DateTime.Now.ToString("dd");

    return $"'{parts[2]}-{parts[1]}-{parts[0]}'";
}
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
kyreSong
  • 1
  • 1
  • 3
  • 2
    Parts isn't a datetime object. You want `DateTime.ParseExact(sDate, "MM/dd/yyyy", System.Globalization.CultureInfo.CurrentCulture);` to get the date as a C# datetime object. If you're using MySQL then you should use the .NET MySQL driver and [parameterized queries](http://stackoverflow.com/questions/652978/parameterized-query-for-mysql-with-c-sharp) (to prevent SQL injection). I'm not sure why you need to deal with dates in a plaintext format. – ProgrammingLlama Mar 29 '17 at 02:55
  • I've added an answer with a correct ParseExact example. – ProgrammingLlama Mar 29 '17 at 03:09

4 Answers4

0

I guess what you're looking for is this:

string sDate = "6/16/1989";
Console.WriteLine(Convert.ToDateTime(sDate).ToString("MM-dd-yyyy"));

Output is this:

06-16-1989

Hope it helps!

mindOfAi
  • 4,412
  • 2
  • 16
  • 27
0

Try this

private static string FormatDate(string sDate)
{
    // "6/16/1989"
    //mysql format 06-16-1989
    DateTime date;
    if (!DateTime.TryParseExact(sDate, new string[] { "MM/dd/yyyy", "M/dd/yyyy", "MM/d/yyyy" }, System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.AdjustToUniversal, out date))
    {
        return string.Empty;
    }
    return date.ToString("MM-dd-yyyy");
}

This would also work if you just wanted to use a standard TryParse:

private static string FormatDate(string sDate)
{
    // "6/16/1989"
    //mysql format 06-16-1989
    var usCultureInfo = System.Globalization.CultureInfo.GetCultureInfo(0x0409);
    DateTime date;
    if (!DateTime.TryParse(sDate, usCultureInfo, System.Globalization.DateTimeStyles.AdjustToUniversal, out date))
    {
        return string.Empty;
    }
    return date.ToString("MM-dd-yyyy");
}

Note that these examples will return an empty string when the date isn't understood.

But do consider using the MySQL driver and parameterized queries since this will make your code safer and it will be easier to work with.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • It doesn't work somehow. So I did this solution and it worked – kyreSong Mar 29 '17 at 17:53
  • private static string FormatDate(string sDate) { // "6/16/1989" //mysql format yyyy-mm-dd => 1989-06-16 string[] parts = sDate.Split('/'); string mm = parts[1]; if (mm.Length < 2) { parts[1] = "0" + mm; } string dd = parts[0]; if (dd.Length < 2) { parts[0] = "0" + dd; } return $"'{parts[2]}-{parts[0]}-{parts[1]}'"; } – kyreSong Mar 29 '17 at 17:54
0

You can use DateTime.ParseExact() which will tell you when the passed in value does not have a correct format by throwing an exception. An example would be

string sDate = "6/6/2016";

        try
        {
            DateTime dt = DateTime.ParseExact(sDate, "d/M/yyyy", CultureInfo.InvariantCulture);

            Console.WriteLine(dt.ToString("yyyy-MM-dd"));
        }
        catch (Exception e) {

            Console.WriteLine(e.Message);
        }
Hong Yi
  • 74
  • 7
-1

First, you can parse the string to a DateTime via:

var dt = DateTime.Parse(sDate);

Then you could use the DateTime.ToString formatting methods, e.g.:

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

Alternatively:

return dt.ToString("MM") + "-" + dt.ToString("dd") + "-" dt.ToString("yy");
TJF
  • 2,248
  • 4
  • 28
  • 43
  • You have to use ParseExact because the date is in a specific format. Consider the UK format dd/MM/yyyy. Is it the third of January or the first of March? – ProgrammingLlama Mar 29 '17 at 03:05
  • Thank you. Yes I need to get the year first that's why I made them as parts. And this didn't work :( – kyreSong Mar 29 '17 at 03:17