C# "String ''2022-11-17 00:00:00'' was not recognized as a valid DateTime."}

1. 问题描述

  • 下面代码报异常: "String ''2022-11-17 00:00:00'' was not recognized as a valid DateTime."}
 Convert.ToDateTime(time).ToString("yyyy-MM-dd HH:mm:ss")

2. 原因分析

  • 这个是客户端传来的,一开始以为是时间值前后有空格,修改了一下不行。后来发现是调用接口中时间值有单引号造成的。
# 原来地址
api/Visitor/AddVisitorToSenseLink?code={item}&dateStart='{this.dateStart.DisplayDate.ToString("yyyy-MM-dd HH:mm:ss")'}

# 修改后地址
api/Visitor/AddVisitorToSenseLink?code={item}&dateStart={this.dateStart.DisplayDate.ToString("yyyy-MM-dd HH:mm:ss")}

你可能感兴趣的:(C# "String ''2022-11-17 00:00:00'' was not recognized as a valid DateTime."})