Valid Date question?

  • Thread starter Thread starter Nicholas Paldino [.NET/C# MVP]
  • Start date Start date
N

Nicholas Paldino [.NET/C# MVP]

William,

Can you be more specific? I mean, the only days you ever have to worry
about are 29, 30, and 31. Checking 30 and 31 are simple, if the month is
not February, then you can just have a lookup array to determine whether or
not the month has 30 or 31 days. If the month is february, and the day is
29, then you have to see if it is a leap year.

Hope this helps.
 
William Stacey said:
Given a day of the month (<=31) how to calculate if that day is valid for
all months up to some end date or DateTime.MaxValue? TIA

I'm not clear what you want. Say, given "30", you want "true" for every
month but February? Given "29", "true" for every month but February during
leap years? What is your interface? Are we given month & year, or just
day of month?

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
Thanks James. I should known better to supply more info to help, so here is
my first stab at it.

Method Sig or Constructor with same sig:

private nextRunDate;

bool IsValid(DateTime runDateTime, DateTime endDateTime, int[] daysOfMonth,
MonthsOfTheYear months)
{
// ?
}

void SetNextRunDate()
{
// Update nextRunDate to nearest rundate and time using daysOfMonth and
MonthsOfTheYear fields that user set.
}

MonthsOfTheYear Enum:
[Flags]
public enum MonthsOfTheYear
{
January = 1,
February = 2,
March = 4,
April = 8,
May = 16,
June = 32,
July = 64,
August = 128,
September = 256,
October = 512,
November = 1024,
December = 2048
}

1) Given the method above, I want to set nextRunDate to the *nearest rundate
after Now (unless it is Now and valid run date, then that should be the
nextRunDate). User can supply days of month to run job and the month(s)
those days will apply to. The months are a Flags enum so you can Or
multiple months together. SetNextRunDate() will set nextRunDate to next
valid day and month carrying over the time. If user supply days or 29-31,
that may not be valid for certain months on certain years. So I guess I
would like to validate that the arguments will all be within range between
runDateTime and endDateTime inclusive. Hope you see what I mean.

2) Also would like a simple way to get DateTime of last day of Next month
and carry over the Time. Probably very simple, but now I am getting punchy
with datetime logic.
 
Thanks Nicholas. Post reply under James C. Cheers!

--
William Stacey, MVP
Can you be more specific? I mean, the only days you ever have to
worry
....
 
Hi William,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to check if a day value is
valid in a certain month. If there is any misunderstanding, please feel
free to let me know.

I think there are several ways to this goal.

1. If the day value is less than 29, it is valid to all months.
2. When day value is larger than 28, we need can first check how many days
are there in a month using DateTime.DaysInMonth. If the day value is larger
than DaysInMonth, that value is invalid.
3. We can also use DateTime.Parse method to check if a day is valid. When
invalid, a FormatException will be thrown.

Unfortunately, we have to iterate through each day for run date to see if
it is valid.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Given a day of the month (<=31) how to calculate if that day is valid for
all months up to some end date or DateTime.MaxValue? TIA
 
Back
Top