01/10/2018, 11:06

Bị lỗi datetime khi build

Tại sao em code dòng:

Console.OutputEncoding = Encoding.UTF8;

Console.WriteLine("Mời bạn nhập vào năm sinh: ");
string s= Console.ReadLine();
DateTime birthday = DateTime.Parse(s);
Console.WriteLine("Bạn sinh ngày: " + birthday.Day);
Console.WriteLine("Bạn sinh tháng: "+birthday.Month);
Console.WriteLine("Bạn sinh năm: " + birthday.Year);
Console.ReadLine();

Lúc em nhập dữ liệu thì nó hiện bảng thồng báo : An unhandled exception of type ‘System.FormatException’ occurred in mscorlib.dll
Additional information: String was not recognized as a valid DateTime.
Mong mấy anh giúp em a. Em cảm ơn nhiều !

Dark.Hades viết 13:20 ngày 01/10/2018

String bạn nhập sai cấu trúc, xem ví dụ tại đây:

docs.microsoft.com

DateTime.Parse Method (System)

Converts the string representation of a date and time to its equivalent.

Bài liên quan
0