The original question asks where is the error? within:
Int64 n = Int64.Parse(DateTime.Today.ToString("dd-MM-yyyy"));
The ToString(...)
method generates a string representation of the date time value. In this case its argument, the string "dd-MM-yyyy"
gives the format of the string to be generated. So today that will generate the string "11-01-2014"
. The Int64.Parse(...)
attempts to parse its argument string as an integer, but here it has a mix of digits and hyphens. Hence it throws an exception.
Understanding these sort of problems can be tricky. One technique is to break the statement into smaller pieces and understand each of them in turn. When the problem is resolved the corrected pieces can be assembled into a single statement if desired. In this case the statement could be split to be:
string s = DateTime.Today.ToString("dd-MM-yyyy");
Console.WriteLine("The date string is '{0}'", s);
Int64 n = Int64.Parse(s);
Then use either a debugger or the WriteLine
shown to show the value in s
. Note that the WriteLine
encloses the displayed value of s
in quotes so that the presence or absence of spaces, newlines and other unexpected characters can easily be detected.