Convert different format of DateTime to specific S

2019-06-01 04:00发布

Im Getting System.datetime.now from different Machine .Each system having different datetime format as a mension below

16-Oct-12 7:25:22 PM
16/10/2012 7:10:47 PM [DD/MM/YYYY]
10/16/2012 7:10:51 PM [MM/DD/YYYY]

How To convert Different format of DateTime to specific String format ?

string sDateTime = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"); 

4条回答
We Are One
2楼-- · 2019-06-01 04:42

The problem of your example is the last two formats 16/10/2012 7:10:47 PM [DD/MM/YYYY] and 10/16/2012 7:10:51 PM [MM/DD/YYYY]. If the value is 10/11/2012 7:20:10 PM how can you know that it is Oct 11, 2012 or Nov 10, 2012?

var input = new string []{ "16-Oct-12 7:25:22 PM",
                "16/10/2012 7:10:47 PM",
                "10/16/2012 7:10:51 PM"};

foreach (var date in input)
{
    var result = DateTime.MinValue;

    if (DateTime.TryParse(date, out result))
    {
        Console.WriteLine("Date value: {0}", result);
    }
    else
    {
        Console.WriteLine("Cannot parse value {0}", date);
    }
}

As you can see, "16/10/2012 7:10:47 PM" could not be parse.

查看更多
劫难
3楼-- · 2019-06-01 04:43
 if (DateTime.TryParse(DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt"), out result))
                    sDateTime = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt");
                else
                {
                    if (System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern.Equals("dd-MMM-yy")) sDateTime = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt");
                    else sDateTime = DateTime.Now.Month +"/" + DateTime.Now.Day+ "/" + DateTime.Now.Year + " " +  DateTime.Now.ToString("hh:mm:ss tt");
                }
查看更多
劫难
4楼-- · 2019-06-01 04:46

First result of Google search: Custom datetime format strings from MSDN

You have to use the line of code you provided explicitly on the other machines when returning the datetime:

string sDateTime = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");]

This is however not aware of time zones! If your application spans several time zones, you should take that into count too!

Or, even better, you could return the Unix time (milliseconds from 1970-01-01) as a long, and of course the timezone info too, if that is different across the machines... (Beware, .NET expoch time is from 0001-01-01 though!)

Returning Epoch time

  • I would recommend against using tryParse: You can not reliably determine if a date is in [DD/MM/YYYY] or [MM/DD/YYYY], only when the month of day is greater than 12... And this would lead to mysterious errors. Believe me, been there, done that (the debugging part)...
查看更多
萌系小妹纸
5楼-- · 2019-06-01 04:51

try the TryParse methode of the Datetime class

Examples from the link:

string[] dateStrings = {"05/01/2009 14:57:32.8", "2009-05-01 14:57:32.8", 
                        "2009-05-01T14:57:32.8375298-04:00", 
                        "5/01/2008 14:57:32.80 -07:00", 
                        "1 May 2008 2:57:32.8 PM", "16-05-2009 1:00:32 PM", 
                        "Fri, 15 May 2009 20:10:57 GMT" };
DateTime dateValue;

Console.WriteLine("Attempting to parse strings using {0} culture.", 
                  CultureInfo.CurrentCulture.Name);
foreach (string dateString in dateStrings)
{
   if (DateTime.TryParse(dateString, out dateValue)) 
      Console.WriteLine("  Converted '{0}' to {1} ({2}).", dateString, 
                        dateValue, dateValue.Kind);
   else
      Console.WriteLine("  Unable to parse '{0}'.", dateString);
}

Notice that in the example its not working for all given strings dates

查看更多
登录 后发表回答