如何将字符串"08:00"转换为DateTime数据类型?
我试图像这样使用它,我得到错误:
public DateTime currentTime
{
get
{
return DateTime.TryParse(
this.Schedule.Timetables.Max(x => x.StartTime),
currentTime);
}
set
{
currentTime = value;
}
}
Run Code Online (Sandbox Code Playgroud)
/ M
您想要查看DateTime.Parse()或DateTime.TryParse().
DateTime.Parse()接受一个字符串,如果失败,将抛出几个异常中的一个.DateTime.TryParse()接受一个字符串和一个out DateTime参数,并返回一个布尔值来确定解析是否成功.
您还会发现您可以使用大多数其他C#struts执行此操作,例如Boolean,Int32,Double等...
使用您拥有的代码
public DateTime CurrentTime
{
get
{
DateTime retVal;
if(DateTime.TryParse(this.Schedule.TimeTables.Max(x => x.StartTime), out retVal))
{
currentTime = retVal;
}
// will return the old value if the parse failed.
return currentTime;
}
set
{
currentTime = value;
}
}
private DateTime currentTime;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
301 次 |
| 最近记录: |