日期时间 - 如何创建一个日期时间月,仅一天。 (无年份) [英] DateTime - How to create a date time with month and day only. (No year)
问题描述
我创建的SharePoint在VS定时器的工作,我想创建一个Date对象只有一个的月和日。这样做的原因是因为我想这个工作的具体日期每年运行。
如果它不可能与日期对象,那么你将如何去这样做?
下面就是我有:
DateTime值=新日期时间(2010年,1,18);
好了,你可以创建自己的类型 - 但一的DateTime
的总是的有一个完整的日期和时间。你甚至不能在使用刚刚有个约会的DateTime
- 你能来最接近的是有一个的DateTime
。在午夜
您总是可以的忽略的一年,但 - 或采取本年度:
//考虑是否要DateTime.UtcNow.Year代替
DateTime值=新日期时间(DateTime.Now.Year,月,日);
要创建自己的类型,你总是可以只嵌入一个的DateTime
A结构内,和代理像 AddDays
等
公共结构MONTHDAY:IEquatable<&MONTHDAY GT;
{
私人只读的DateTime日期时间;
公共MONTHDAY(INT月,日整型)
{
日期时间=新的datetime(2000年,月,日);
}
公共MONTHDAY AddDays(INT天)
{
的DateTime添加= dateTime.AddDays(天);
返回新MONTHDAY(added.Month,added.Day);
}
// TODO:实现接口,平等等
}
请注意,您选择在今年影响了类型的行为 - 应2月29日是一个有效的月/日的价值或没有?这取决于今年...
我个人不认为我的将会的创建这种类型 - 而不是我有一个方法返回下一次程序应该运行。
I am creating a timer job in VS for sharepoint, and I want to create a Date object that only has a month and day. The reason for this is because I want this job to run annually on the specific date.
If it's not possible with a date object, then how would you go about doing this?
Here's what I've got:
DateTime value = new DateTime(2010, 1, 18);
Well, you can create your own type - but a DateTime
always has a full date and time. You can't even have "just a date" using DateTime
- the closest you can come is to have a DateTime
at midnight.
You could always ignore the year though - or take the current year:
// Consider whether you want DateTime.UtcNow.Year instead
DateTime value = new DateTime(DateTime.Now.Year, month, day);
To create your own type, you could always just embed a DateTime
within a struct, and proxy on calls like AddDays
etc:
public struct MonthDay : IEquatable<MonthDay>
{
private readonly DateTime dateTime;
public MonthDay(int month, int day)
{
dateTime = new DateTime(2000, month, day);
}
public MonthDay AddDays(int days)
{
DateTime added = dateTime.AddDays(days);
return new MonthDay(added.Month, added.Day);
}
// TODO: Implement interfaces, equality etc
}
Note that the year you choose affects the behaviour of the type - should Feb 29th be a valid month/day value or not? It depends on the year...
Personally I don't think I would create a type for this - instead I'd have a method to return "the next time the program should be run".
这篇关于日期时间 - 如何创建一个日期时间月,仅一天。 (无年份)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!