[英]Getting a days collection from System.DateTime for a given month in custom format in WPF/C#
I am looking to get a list of days in the following format: Jan 1, Jan 3.....Jan 30, Jan 31. 我希望以以下格式获取天数列表:1月1日,1月3日..... 1月30日,1月31日。
I can generate this collection by my self by calling DateTime.DaysInMonth(2013,01); 我可以通过调用DateTime.DaysInMonth(2013,01);自行生成此集合。 But this just gives me "31" so i will have to loop through them and generate collection.
但这只给我“ 31”,因此我将不得不遍历它们并生成集合。 But will there be any custom methods already that does this easily or in simple way.
但是已经有任何自定义方法可以轻松或简单地实现此目的。
Thanks 谢谢
Try to use next code snippet. 尝试使用下一个代码段。 Consider using the
MMM
DateTime pattern to extract month name 考虑使用
MMM
DateTime模式提取月份名称
Enumerable.Range(1, DateTime.DaysInMonth(2012, 1))
.Select(num => num +" Jan");
returns 退货
1 Jan
2 Jan
3 Jan
...
30 Jan
31 Jan
Edit: To extract month name you can use next code snippet 编辑:要提取月份名称,可以使用下一个代码段
var start = new DateTime(2012, 1, 1);
Enumerable.Range(0, DateTime.DaysInMonth(2012, 1))
.Select(days => start.AddDays(days))
.Select(day => day.ToString("d MMM")); // returns the same
I don't think there are any built in methods to do this, but you could easily do something like: 我认为没有任何内置方法可以执行此操作,但是您可以轻松地执行以下操作:
DateTime.DaysInMonth(2013, 01).Select(x=> "Jan " + x.Tostring()).ToList();
You can see how you can easily parameterize and make your own function out of this to get you the different months as well. 您将看到如何轻松地进行参数设置并利用此功能来获得不同的月份。
根据上述Ilya Ivanov的答案,如果您希望列表显示格式为“ Jan ??”,则可以执行以下操作:
var list = Enumerable.Range(1, DateTime.DaysInMonth(2012, 1)).Select(num => "Jan " + num);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.