visual Studio 2017 年的日期时间选择器
datetimepicker in visual Studio 2017
我的 C# 代码有问题。
string searchstring = " and UsersStartDate Between '" + String.Format("{0:yyyy/MM/dd}",
Convert.ToDateTime(calender_from.Text))
+ "' And'" + String.Format("{0:yyyy/MM/dd}",
Convert.ToDateTime(calender_till.Text))+ "'";
我为日期选择器编写了这段代码,并将显示集设置为 1/1/2015,运行 时间出现错误。 "System.FormatException: 'String was not recognized as a valid DateTime'
。我正在关注 C# 培训视频,视频中的人回答了我遇到的这个错误。
您的字符串是 1/1/2015
,所以这是 day/month/year 或 month/day/year(没有前导零)。
但是您的格式字符串是 yyyy/MM/dd
,即 year/month/day(前导零)。
使用与您的输入匹配的正确格式字符串。对于 1/1/2015
,它可能是 M/d/yyyy
或 d/M/yyyy
。您可以将此格式传递给 DateTime.ParseExact.
见Custom DateTime Format Strings。
using System.Globalization;
string dateString = "1/1/2015";
string format = "d/M/yyyy";
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime result = DateTime.ParseExact(dateString, format, provider);
我的 C# 代码有问题。
string searchstring = " and UsersStartDate Between '" + String.Format("{0:yyyy/MM/dd}",
Convert.ToDateTime(calender_from.Text))
+ "' And'" + String.Format("{0:yyyy/MM/dd}",
Convert.ToDateTime(calender_till.Text))+ "'";
我为日期选择器编写了这段代码,并将显示集设置为 1/1/2015,运行 时间出现错误。 "System.FormatException: 'String was not recognized as a valid DateTime'
。我正在关注 C# 培训视频,视频中的人回答了我遇到的这个错误。
您的字符串是 1/1/2015
,所以这是 day/month/year 或 month/day/year(没有前导零)。
但是您的格式字符串是 yyyy/MM/dd
,即 year/month/day(前导零)。
使用与您的输入匹配的正确格式字符串。对于 1/1/2015
,它可能是 M/d/yyyy
或 d/M/yyyy
。您可以将此格式传递给 DateTime.ParseExact.
见Custom DateTime Format Strings。
using System.Globalization;
string dateString = "1/1/2015";
string format = "d/M/yyyy";
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime result = DateTime.ParseExact(dateString, format, provider);