Cannot convert from 'string' to 'System.IFormatProvider'

This code gives me this error:

var n = "9/7/2014 8:22:35 AM";
var m = n.ToString("yyyy-MM-dd'T'HH:mm:ssZ");

But this code works as it should, and returns the date in the appropriate format.

var n = DateTime.Now;
var m = n.ToString("yyyy-MM-dd'T'HH:mm:ssZ");

Does anyone know why the first code does not work and how to make it work?

+5
source share
2 answers

You need to understand how static printing works. In the first case, the type nis equal string. The type stringhas a method ToString(), but this method takes no arguments and returns the same string object or accepts a format provider. Since you provided an argument, the compiler assumes that you had in mind the second version, but the types do not match.

, , , , , DateTime Parse TryParse:

var n = DateTime.Parse("9/7/2014 8:22:35 AM");

a string DateTime. n - DateTime.

, var, #. , , , . , . DateTime. IFormatProvider, . :

DateTime n = "9/7/2014 8:22:35 AM";
string m = n.ToString("yyyy-MM-dd'T'HH:mm:ssZ");

1, , ( "9/7/2014 8:22:35 AM" ) string DateTime, .

+14
var n = "9/7/2014 8:22:35 AM";

. ,

var n = DateTime.Parse("9/7/2014 8:22:35 AM");

var n = DateTime.Now;

DateTime

+1

All Articles