简体   繁体   中英

Convert string to datetime in any format

Aim :

want to convert date time in any format to de-DE culture

Sample Datetime :

3/30/2016 2:38:20 PM

4/4/2016 11:08:10 AM

What I tried

protected void Page_Load(object sender, EventArgs e)
{
    string dt1Valid = CheckDateFormat("3/30/2016 2:38:20 PM");
}

public string CheckDateFormat(string checkDate)
{
    string formats1 = getFormat(checkDate);

    DateTime parsedDateTime;
    DateTimeFormatInfo ukDtfi = new CultureInfo("de-DE", false).DateTimeFormat;
    if (!DateTime.TryParseExact(checkDate, formats1, new CultureInfo("de-DE"),
                                   DateTimeStyles.None, out parsedDateTime))
    {
        return Convert.ToDateTime(parsedDateTime.ToString()).ToString(ukDtfi.ShortDatePattern + " " + ukDtfi.LongTimePattern);

    }
    else
        return "";
}
public string getFormat(string checkDate)
{
    string[] formats = {"M/d/yyyy", "MM/dd/yyyy",                                    
                    "d/M/yyyy", "dd/MM/yyyy", 
                    "yyyy/M/d", "yyyy/MM/dd",
                    "M-d-yyyy", "MM-dd-yyyy",                                    
                    "d-M-yyyy", "dd-MM-yyyy", 
                    "yyyy-M-d", "yyyy-MM-dd",
                    "M.d.yyyy", "MM.dd.yyyy",                                    
                    "d.M.yyyy", "dd.MM.yyyy", 
                    "yyyy.M.d", "yyyy.MM.dd",
                    "M,d,yyyy", "MM,dd,yyyy",                                    
                    "d,M,yyyy", "dd,MM,yyyy", 
                    "yyyy,M,d", "yyyy,MM,dd",
                    "M d yyyy", "MM dd yyyy",                                    
                    "d M yyyy", "dd MM yyyy", 
                    "yyyy M d", "yyyy MM dd",

                    "M/d/yyyy hh:mm:ss tt", "MM/dd/yyyy hh:mm:ss tt",                                    
                    "d/M/yyyy hh:mm:ss tt", "dd/MM/yyyy hh:mm:ss tt", 
                    "yyyy/M/d hh:mm:ss tt", "yyyy/MM/dd hh:mm:ss tt",
                    "M-d-yyyy hh:mm:ss tt", "MM-dd-yyyy hh:mm:ss tt",                                    
                    "d-M-yyyy hh:mm:ss tt", "dd-MM-yyyy hh:mm:ss tt", 
                    "yyyy-M-d hh:mm:ss tt", "yyyy-MM-dd hh:mm:ss tt",
                    "M.d.yyyy hh:mm:ss tt", "MM.dd.yyyy hh:mm:ss tt",                                    
                    "d.M.yyyy hh:mm:ss tt", "dd.MM.yyyy hh:mm:ss tt", 
                    "yyyy.M.d hh:mm:ss tt", "yyyy.MM.dd hh:mm:ss tt",
                    "M,d,yyyy hh:mm:ss tt", "MM,dd,yyyy hh:mm:ss tt",                                    
                    "d,M,yyyy hh:mm:ss tt", "dd,MM,yyyy hh:mm:ss tt", 
                    "yyyy,M,d hh:mm:ss tt", "yyyy,MM,dd hh:mm:ss tt",
                    "M d yyyy hh:mm:ss tt", "MM dd yyyy hh:mm:ss tt",                                    
                    "d M yyyy hh:mm:ss tt", "dd MM yyyy hh:mm:ss tt", 
                    "yyyy M d hh:mm:ss tt", "yyyy MM dd hh:mm:ss tt"

                   };

    DateTime dateValue;

    foreach (string dateStringFormat in formats)
    {
        if (DateTime.TryParseExact(checkDate, dateStringFormat,
                                   CultureInfo.InvariantCulture,
                                   DateTimeStyles.None,
                                   out dateValue))
            //Console.WriteLine("Converted '{0}' to {1}.", dateStringFormat, dateValue.ToString("yyyy-MM-dd"));                
            return dateStringFormat;
    }
    return null;
}

Result i expect

30.03.2016 14:38:20
04.04.2016 11:08:10

Result i get

01.01.0001 00:00:00
01.01.0001 00:00:00

That's simple to answer: It's not possible, so stop trying.

Proof: Any format includes Mdyyyy as well as dMyyyy . In that case, 1.2.2016 can represent either

  • the first of February or
  • the second of January.

Conclusion: A translation from any format is not possible, since it's not possible to parse ambiguous dates without additional information.

Note: This has nothing to do with C# or your technology of choice. Your requirements are broken. Fix them.

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