简体   繁体   中英

Convert custom date to mysql datetime

I have a custom date format that I want to convert to Datetime so I can then insert into my database, I tried using Datetime.ParseExact() But I think I'm misunderstanding something as the code throws a System.FormatException .

I have the following date format from a csv

> 6/11/2014 9:00

and I wish to convert it to the mysql datetime format

> 0000-00-00 00:00:00 OR yyyy-MM-dd HH:mm:ss

Notice they haven't included the seconds in the original date so I am unsure (without appending them to the end) how to set all records to just have " 00 " for seconds as it is not available.

I tried the following which throws an exception

DateTime myDate = DateTime.ParseExact("6/11/2014 9:00", "yyyy-MM-dd HH:mm",
                                   System.Globalization.CultureInfo.InvariantCulture);

first thing you need to convert string to date time and than convert datetime tos tring

string strd = "6/11/2014 9:00";
DateTime dt ;
//convert datetime string to datetime
if(DateTime.TryParse(strd, out dt))
{
  //convert datetime to custom datetime format 
  Console.WriteLine("The current date and time: {0: yyyy-MM-dd HH:mm:ss}", 
                   dt); ;
}

output

在此处输入图片说明

I know this is late to answer that but I'm really surprised none of answer consider to use IFormatProvider to prevent a possible parsing error because of / format specifier or considering your string is a standard date and time format for your CurrentCulture or not so you can or can't use DateTime.TryParse(string, out DateTime) overload directly.

First of all, let's look at what DateTime.ParseExact documentation says:

Converts the specified string representation of a date and time to its DateTime equivalent. The format of the string representation must match a specified format exactly or an exception is thrown.

In your case, they don't match. You should use d/MM/yyyy H:mm format to parse your example string with a culture that have / as a DateSeparator . I almost always suggest to use DateTime.TryParseExact method in this kind of situations;

string s = "6/11/2014 9:00";
DateTime dt;
if(DateTime.TryParseExact(s, "d/MM/yyyy H:mm", CultureInfo.InvariantCulture,
                          DateTimeStyles.None, out dt))
{
    Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss"));
    // result will be 2014-11-06 09:00:00
}

If you know formats of your dates, then you can do this:

string stringDate = "6/11/2014 9:00";
//Your date formats of input
string[] dateFormats = new string[] 
{ 
    "d/MM/yyyy H:mm", 
    "dd/MM/yyyy H:mm", 
    "dd/MM/yyyy HH:mm", 
    "dd/MM/yyyy H:mm:ss", 
    "dd/MM/yyyy HH:mm:ss" 
    /* And other formats */ 
};

DateTime convertedDate;
bool isSuccessful = DateTime.TryParseExact(stringDate, dateFormats,
    System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out convertedDate);

if (isSuccessful)
{
    //If conversion was successful then you can print your date at any format you like
    //because you have your date as DateTime object
    Console.WriteLine(convertedDate.ToString("dd-MM-yyyy HH:mm:ss")); /* Or other format you want to print */
}

I hope it will be helpful to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM