C # - Am I replacing perfectly good code?

I had a quick date formatting function, here it is:

public static string archiveServerDateTime(string datetime)
    {
        DateTime tempDateTime = DateTime.ParseExact(datetime,"dd.MM.yyyy HH:mm:ss", null);
        return tempDateTime.ToString("yyyy/MM/dd:HH:mm:ss");
    }

to find the result of the function = 2009.10.22: 16: 21: 03, and it is surprising that this is only on 1 production server, the test server worked perfectly ...

So now I rewrote the function in the old school style:

public static string archiveServerDateTime(string datetime)
    {
        DateTime tempDateTime = DateTime.ParseExact(datetime,"dd.MM.yyyy HH:mm:ss", null);
        string yearPart = Convert.ToString(tempDateTime.Year);
        string monthPart = Convert.ToString(tempDateTime.Month).PadLeft(2,'0');
        string dayPart = Convert.ToString(tempDateTime.Day ).PadLeft(2, '0');
        string hourPart = Convert.ToString(tempDateTime.Hour).PadLeft(2, '0');
        string minutePart = Convert.ToString(tempDateTime.Minute).PadLeft(2, '0'); 
        string secondPart = Convert.ToString(tempDateTime.Second).PadLeft(2,'0');

        return yearPart + @"/" + monthPart + @"/" + dayPart + ":" + hourPart + ":" + minutePart + ":" + secondPart; 
        //return tempDateTime.ToString("yyyy/MM/dd:HH:mm:ss");
    }

So, I ask you, ladies and gentlemen, have I replaced completely good code, or is this a Microsoft bug? Can we really trust these new language features, which apparently aren't that strong, or am I just missing something?

+3
source share
2 answers

DateTimeFormatInfo.InvariantInfo DateTime.ToString. / "/" . :

return tempDateTime.ToString("yyyy/MM/dd:HH:mm:ss", DateTimeFormatInfo.InvariantInfo);

, ToString Parse ( DateTime s). FxCop (VS Code Analysis) , , . , .

+26

. . .

: . 2009.10.31

+4

Source: https://habr.com/ru/post/1721601/


All Articles