简体   繁体   English

从WPF / C#中以自定义格式从System.DateTime获取给定月份的天收集

[英]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

You can use Linq: 您可以使用Linq:

DateTime date = new DateTime(2013, 1, 1);
var cal = System.Globalization.CultureInfo.CurrentCulture.Calendar;
List<String> monthDates = Enumerable.Range(0, cal.GetDaysInMonth(date.Year, date.Month))
    .Select(i => date.AddDays(i).ToString("MMM d"))
    .ToList();

Demo 演示版

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.

相关问题 无法将“字符串”转换为“System.DateTime” C# WPF - Cannot convert 'string' to 'System.DateTime' C# WPF C#-System.FormatException:System.DateTime处的格式字符串无效 - C# - System.FormatException: Invalid format string at System.DateTime 从asp.net(C#)System.dateTime插入/更新SQL DateTime - Insert/Update SQL DateTime From asp.net(C#) System.dateTime C#模型验证抛出“无法从System.DateTime转换为System.Array” - C# Model validation throws “Cannot convert from System.DateTime to System.Array” 为什么C#中没有System.DateTime的“日期”简写? - Why is there no "date" shorthand of System.DateTime in C#? C#无法从&#39;System.DateTime&#39;转换为&#39;object []&#39;methodinfo.invoke - C# Cannot convert from 'System.DateTime' to 'object[]' methodinfo.invoke 如何在C#codeDom中为System.DateTime使用CodeObjectCreateExpression - How to use CodeObjectCreateExpression for System.DateTime in C# codeDom 无法在C#中将类型字符串隐式转换为system.datetime - cannot implicitly convert type string to system.datetime in c# 错误-无法从“ System.DateTime转换?” &#39;串&#39;C# - error-cannot convert from 'System.DateTime?' to 'string' C# C#无法将类型&#39;string&#39;隐式转换为&#39;System.DateTime&#39; - C# Cannot implicitly convert type 'string' to 'System.DateTime'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM