[英]Can't convert DateTime to string
I'm trying to convert a DateTime
to a string but actually the dt
variable passed in the getFixtures
method is underlined in red. 我正在尝试将
DateTime
转换为字符串,但实际上在getFixtures
方法中传递的dt
变量以红色下划线。
DateTime dt = DateTime.ParseExact(Calendar.SelectedDate.Value.ToString(), "ddMMyyyy",
CultureInfo.InvariantCulture);
dt.ToString("ddMMyyyy");
fixtures.getFixtures(dt);
Error: 错误:
Can't convert DateTime to string.
无法将DateTime转换为字符串。
What am I doing wrong? 我究竟做错了什么?
Seems you want pass a string to the getFixtures
but actually you are passing the dt
which is a DateTime
. 看起来你想把一个字符串传递给
getFixtures
但实际上你传递的是一个DateTime
的dt
。
dt.ToString("ddMMyyyy");
does not change the dt
to string
, so you should save it's output to a string and use that string. 不会将
dt
更改为string
,因此您应将其输出保存为字符串并使用该字符串。 Here is the code: 这是代码:
var stringDt = dt.ToString("ddMMyyyy");
fixtures.getFixtures(stringDt );
Your code: 你的代码:
dt.ToString("ddMMyyyy");
fixtures.getFixtures(dt);
If you want to pass string representation of datetime inside a method, change it to : 如果要在方法中传递datetime的字符串表示形式,请将其更改为:
fixtures.getFixtures(dt.ToString("ddMMyyyy"));
instead of, dt.ToString("ddMMyyyy"); fixtures.getFixtures(dt);
而不是,
dt.ToString("ddMMyyyy"); fixtures.getFixtures(dt);
dt.ToString("ddMMyyyy"); fixtures.getFixtures(dt);
use 采用
string dttime=dt.ToString("ddMMyyyy");
fixtures.getFixtures(dttime);
using dt.ToString("ddMMyyyy")
does not converts dt to string, it just returns a new string, dt remains datetime as it was. 使用
dt.ToString("ddMMyyyy")
不会将dt转换为字符串,它只返回一个新字符串,dt保持日期时间。
This is an old way but it works. 这是一种古老的方式,但它的工作原理。 I added to the model.
我加入了模型。
private string _displayDate; private string _displayDate;
[Display(Name="Display Date")]
[DataType(DataType.Text)]
public string DisplayDate
{
get
{
DateTime dt = new DateTime();
dt = Convert.ToDateTime(_displayDate);
return dt.ToShortDateString();
}
set { _displayDate = value; }
}
//Displays: 3/2/1996 //显示:3/2/1996
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.