Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 C#:将字段转换为月份和年份格式_C# 4.0 - Fatal编程技术网

C# 4.0 C#:将字段转换为月份和年份格式

C# 4.0 C#:将字段转换为月份和年份格式,c#-4.0,C# 4.0,我有下面的代码行 txtCustomers.Text = _myCustList[0].F5Customer.dateActive; _myCustList[0].F5Customer.dateActive returns : 2/27/2014 12:00:00AM 我希望日期格式为[月,年] 我尝试了下面的代码,但没有成功 txtCustomers.Text = _myCustList[0].F5Customer.dateActive.ToString("MMMM , YYYY"); 我

我有下面的代码行

txtCustomers.Text = _myCustList[0].F5Customer.dateActive;
_myCustList[0].F5Customer.dateActive returns : 2/27/2014 12:00:00AM
我希望日期格式为[月,年]

我尝试了下面的代码,但没有成功

txtCustomers.Text = _myCustList[0].F5Customer.dateActive.ToString("MMMM , YYYY");
我怎样才能得到[月,年]格式。

什么是“月,年”格式?也许您需要完整的月份名称和年份,然后使用小写的
yyyy
,因此
“MMMM,yyyy”
而不是
“MMMM,yyyy”

编辑:正如注释所述,属性是
字符串
而不是
日期时间
。为什么称为
dateActive
的属性首先是字符串?我会把它变成一个
DateTime
属性。否则,您必须始终将其解析为
DateTime

txtCustomers.Text = DateTime.Parse(_myCustList[0].F5Customer.dateActive).ToString("MMMM , yyyy");

对填写月份名称和年份。代码行不起作用。@user2320476:我已经测试过了,
DateTime.Now.ToString(MMMM,yyyy)
为我返回了
2014年11月
。什么意思是“完全不工作”?ToString方法有0个参数,但用1个参数调用是我收到的错误消息。@user2320476:那么我猜
dateActive
是一个可为空的日期时间或字符串,而不是
datetime
,这是什么?如果是
日期时间
如果您确定所有文件都有一个值,则可以使用
dateActive.Value.ToString…
。dateActive是一个字符串
txtCustomers.Text = DateTime.Parse(_myCustList[0].F5Customer.dateActive).ToString("MMMM , yyyy");