我从 soql 语句中动态提取日期字段。在我的中继器中,我得到了 closeDate 值。我想我会把年份拉到我的集合中:目前我正在提取数据,例如:“2019-12-31”,我只想显示“2019”private String closedateyear;public String CloseDateYear { get { return closedateyear; } set { //pull year }}从我读到的内容来看,这样的内容可能有效:string myDate = "05/11/2010"; DateTime date = Convert.ToDateTime(myDate);int year = date.Year;但是当我实现它时它显示 null感谢任何帮助或指导
2 回答
泛舟湖上清波郎朗
TA贡献1818条经验 获得超3个赞
使用DateTime.Parse或DateTime.ParseExact从字符串到日期时间的转换更容易
所以,你可以试试这个:
string year = DateTime.Parse("2019-12-31").Year.ToString();
噜噜哒
TA贡献1784条经验 获得超7个赞
我的问题是我在设置中拉取了 closeateyear 的空值。一旦我设置值 closedateyear = value; 工作后的一切:
private String closedateyear;
public String CloseDateYear
{
get { return closedateyear; }
set
{
closedateyear = value;
//retrieving data "2019-12-31"
DateTime date = Convert.ToDateTime(closedateyear);
//convert closedateyear to date time: similar to: "12/31/2019 12:00:00 AM"
int year = date.Year;
//extracting year: "2019"
closedateyear = year.ToString();
//set closedateyear = year value "12/31/2019 12:00:00 AM"
}
}
- 2 回答
- 0 关注
- 115 浏览
添加回答
举报
0/150
提交
取消