Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a date time with month and day only, no year

Tags:

c#

datetime

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);
like image 549
novicePrgrmr Avatar asked Oct 31 '11 19:10

novicePrgrmr


People also ask

How do I create a specific date in C#?

To set dates in C#, use DateTime class. The DateTime value is between 12:00:00 midnight, January 1, 0001 to 11:59:59 P.M., December 31, 9999 A.D. Let's create a DateTime object.

How can I get only the date from DateTime format?

ToString() − One more way to get the date from DateTime is using ToString() extension method. The advantage of using ToString() extension method is that we can specify the format of the date that we want to fetch. DateTime. Date − will also remove the time from the DateTime and provides us the Date only.

How do you instantiate DateTime?

To instantiate a DateTime value by using the year, month, and day in another calendar, call the DateTime(Int32, Int32, Int32, Calendar) constructor. The time of day for the resulting DateTime is midnight (00:00:00). The Kind property is initialized to DateTimeKind.


3 Answers

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".

like image 139
Jon Skeet Avatar answered Oct 19 '22 17:10

Jon Skeet


How about creating a timer with the next date?

In your timer callback you create the timer for the following year? DateTime has always a year value. What you want to express is a recurring time specification. This is another type which you would need to create. DateTime is always represents a specific date and time but not a recurring date.

like image 27
Alois Kraus Avatar answered Oct 19 '22 16:10

Alois Kraus


There is no such thing like a DateTime without a year!

From what I gather your design is a bit strange:

I would recommend storing a "start" (DateTime including year for the FIRST occurence) and a value which designates how to calculate the next event... this could be for example a TimeSpan or some custom structure esp. since "every year" can mean that the event occurs on a specific date and would not automatically be the same as saysing that it occurs in +365 days.

After the event occurs you calculate the next and store that etc.

like image 1
Yahia Avatar answered Oct 19 '22 17:10

Yahia