Parsing impossible dates in C# - c#

Is there an elegant way to be permissive in date input in C# to accommodate user input like '2009-09-31' (e.g. September 31, which doesn't exist and causes DateTime.Parse to choke)? Ideally I would like to parse this as October 1 (e.g. latest possible date plus overflow).

I don't believe this is handled directly for you. What you could do is parse the date yourself, as three separate integers:
Parse the 2009, and construct a DateTime of 1st January 2009
Parse the 09 and subtract one, then call dt = dt.AddMonths(8) to get September 1st
Parse the 31 and subtract one, then call dt.AddDays(30)
This will handle things like 2009/13/01 to mean 1st January 2010. It won't do what you want with February 31st though, I suspect...

One way to avoid incorrect dates in User input, is not to let them happen in the first instance, i.e. use a date picker control.
What about using DateTime.TryParse ?
Converts the specified string
representation of a date and time to
its DateTime equivalent using the
specified culture-specific format
information and formatting style, and
returns a value that indicates whether
the conversion succeeded.
This MSDN page shows examples of usage.

I think the DateTime.DaysInMonth method can help a lot, after a TryParse ... you can implement the logic you are talking about

You should (morally) not try to handle inputs like this. Is February 29, 1900 to be interpreted as March 1, 1900 (because February 29, 1900 could be interpreted as the day after February 28, 1900 but since it doesn't exist move the actual day after February 28, 1900) or as February 28, 1900 (because February 29, 1900 could be interpreted as the last day of February, 1900)? Another situation is what if the user means to type "2009-3-3" but because of fast and sticky fingers accidentally types "2009-3-33". Then rather than their error being caught, a custom parser will swallow this into, say, 4/2/2009. Because of situations like this, you should just DateTime.TryParse the input and inform the user if invalid input occurs. That's what you should do.
Now, if it's a requirement that you handle such input. I would use something along the lines of the following:
static DateTime Parse(int year, int month, int day) {
DateTime date;
if (month < 1 || month > 12) {
int direction = month < 1 ? 1 : -1;
do {
month += direction * 12;
year -= direction;
} while (month < 1 || month > 12);
}
int daysInMonth = DateTime.DaysInMonth(year, month);
if (day < 1 || day > daysInMonth) {
date = new DateTime(year, month, daysInMonth);
int difference = day - daysInMonth;
date = date.AddDays(difference);
}
else {
date = new DateTime(year, month, day);
}
return date;
}

The compiler will just throw a cannot parse exception, so you'll have to write your own parser for this kind of problems.

Rule #1: As reasonably as possible, don't allow a user to input invalid data in the first place.
If you're doing Windows Forms, use the DateTimePicker as much as possible (although it has its limitations when it comes to locale); or, if your app is in ASP.NET, use a Calendar, or one of the AJAX Control Toolkit controls (I forget the exact name right now), but it does a little popup with a calendar.
I would strongly advise against separating out the date components into different fields (or parsing them as such) if there is ANY chance of needing to localize this application later.

Related

How to set Date limit in days?

I have been trying to set a time limit for a certain form to not function past a specified date in a month, but I have been unsuccessful so far.
I am working with a low-code windows interface software that does the majority of the actual coding in the background but since it has some limitations I need to code this Date limit in myself.
The best thing I reached after looking around was this:
DateTime temp = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 15);
And I would add a rule in the program that the date shouldn't be higher than the above.
But for some reason it doesn't work giving me an "; expected" error (in line 1 char 29).
This will only open the specified form up to and including the 15th of the month:
if (DateTime.Now.Day <= 15)
{
myForm.Show();
}
You can obviously change the operator and/or value to change the way it works. The important point is the Day property of the DateTime value representing the current date.
you can use the below code. you have to use AddDays method.
DateTime now = DateTime.Now;
DateTime pastTime = now.AddDays(-15);
DateTime futureTime = now.AddDays(15);
It will give you backdate if you provide a negative value.
you also have a typo in your code, the correct DataType is DateTime.
you can comparisons as you have for int, and doubles.
if(now < pastTime)
{
}
The Error:
"; expected" error (in line 1 char 29).
You're missing a ending semi-colon, probably in a line above or below.
Tip: try to name variables well with meaning especially in anything-but-code (ABC) environments:
DateTime avoidReservedKeyWords = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 15);

c#: How do I get the previous year-to-date and previous month-to-date for specific time periods?

Using c#, I want to compare the current week-to-date to the same period last week-to-date. For example, if today is Wednesday, and if the first day of the week is Sunday, then I want to compare totals for Sunday – Tuesday of this week against Sunday – Tuesday of last week. I’m not counting Wednesday because I don’t have a full day of data until midnight the same day.
The same applies to comparing this mtd to the same number of days last month, and last year. For example, if the current date is June 19th, I want to compare the data from May 1-18th of last month as well as January 1 – June 18th of last year against January 1 – June 18th of this year.
The variables I’m trying to use look like this:
//Current dates
DateTime currentDte = DateTime.Now;
DateTime beginWeek = DateTime.Now.StartOfWeek(DayOfWeek.Sunday);
DateTime beginMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
DateTime beginYear = new DateTime(DateTime.Now.Year, 1, 1);
//Historical dates
DateTime lastWeekToDate = DateTime.Now.StartOfWeek(DayOfWeek.Sunday - (7 - (int)DateTime.Now.DayOfWeek));
DateTime lastMonthToDate =
DateTime lastYearToDate =
As you can see I figured out the current dates and can loop through them to get the wtd, mtd, and ytd data I need. And I managed to figure out how to get the last week-to-date I need.
But I don’t know how to get the lastMonthToDate and lastYearToDate dates I need. I’ve tried everything I can think of. I’ve read date documentation until my eyes hurt, and still I come up with goose eggs. Can anyone offer any suggestions?
If I understand your question correctly, you just need to use AddMonths and AddYears.
DateTime lastMonthToDate = beginMonth.AddMonths(-1);
DateTime lastYearToDate = beginYear.AddYears(-1);
EDIT
Based your comment - it looks like you want the to subtract months/years from the current date in which case it would be
DateTime lastMonthToDate = currentDte.AddMonths(-1);
DateTime lastYearToDate = currentDte.AddYears(-1);
If it's the time of day that's throwing you off, just use the date part DateTime.Now.Date.AddMonths(-1)
Based on the feedback I received from Zeph, I was able to get the beginning of last year using this code:
DateTime startDate = beginYear.AddYears(-1);
Now I'm able to tally all of the ytd data I need based on the interval dates. Thank you very much!

Iterating through a TimeSpan and running an action every month (or arbitrary unit of time)

I'm writing a search program that includes a date range- DateFrom and DateTo, both of which are DateTimes. Searching January - April for any search criteria will return the results of January + February + March + April.
I would like to add functionality whereby a user can choose to search each month within a - range, so searching January - April will return each individual month's results. However I'm having trouble finding an intelligent way to implement this for any unit of time larger than days.
So far I'm getting a TimeSpan using:
TimeSpan ts = query.DateTo - query.DateFrom;
In a perfect world I'd just be able to do something like foreach (month m in TimeSpan){dostuff}, however TimeSpan stores dates as integers and does not include any units larger than days. Additionally, I thought maybe I could just use n = DateFrom.month - DateTo.month to get the difference in months and run a function in a for loop starting with DateFrom and lasting n months, but this won't work between years.
The last case is definitely fixable but includes a number of tedious special cases. Is there a cleaner / more elegant way of accomplishing this sort of iteration that I'm missing?
Thanks.
So for the basic pattern we can use a fairly simple for loop:
public static IEnumerable<DateTime> Months(DateTime start, DateTime end)
{
for (DateTime date = start; date < end; date = date.AddMonths(1))
yield return date;
}
Now in this case we have a start date that is inclusive and an end date that is exclusive. If we want to make the end date inclusive, as you have described, we can add:
end = end.AddMonths(1);
to the start of the method.
Next you have a few other considerations. Are the datetime objects passed in going to always be the first of the month? If not, how do you want to support it? If the start date is Feb 10th do you want the first yielded date to be Feb 1st (the start of the start date's month), March 1st (the first "first day of the month" on or after the start date), or Feb 10th (meaning that each date in the timespan would be the 10th day of that month)?
Those same questions also apply to the end date; do you want the last "first day of the month" before the end date, the first day of the next month, etc.
Also, what should happen if the start date is after the end date? Should it yield the dates "backwards", should it just pretend the start date is the end date and the end date is the start date? Should it keep adding days until you've overflowed DateTime and come back around to that date?
Pretty much all of these issues aren't too hard to deal with, the hard part is just knowing what you want to do in each case.
You could do something like:
var months = ((query.DateTo.Year - query.DateFrom.Year) * 12) + query.DateTo.Month - query.DateFrom.Month
for(int i=0;i<months;i++){
//do stuff as below
var currentDate=query.DateFrom.AddMonths(i);
}

How can I make a date time picker always contain the last day of the month while showing month and year only?

I have a DateTimePicker which I have set to only show the month and year as follows:
myDateTimePicker.Format = DateTimePickerFormat.Custom;
myDateTimePicker.CustomFormat = "MMMM yyyy";
myDateTimePicker.ShowUpDown = true;
However, I want the value of the date to always be the last day of the selected month, so I set the DateTime in the ValueChanged event using:
DateTime selectedDate = myDateTimePicker.Value;
DateTime lastDayOfMonth = new DateTime(
selectedDate.Year,
selectedDate.Month,
DateTime.DaysInMonth(selectedDate.Year, selectedDate.Month));
myDateTimePicker.Value = lastDayOfMonth;
The problem is, if I have a month like March selected, and I change the month to February using the up/down controls, I get the following error before I can handle the ValueChanged event:
ArgumentOutOfRangeException was unhandled
Year, Month, and Day parameters describe an un-representable DateTime.
This is understandable because the date was 31 March and it is being changed to 31 February which is an invalid date. However, I want to change it to 28 February (or 29 February).
How can I achieve this?
Very strange, I tried to reproduce your problem, and when I switch from March to February, my control just doesn't display anything...
Maybe we have different framework versions, who handle the same error differently.
Anyway, one solution would be to set the datetime picker to the first of each month, and when you need the value, you can just use your code as it currently is:
DateTime lastDayOfMonth = new DateTime(
selectedDate.Year,
selectedDate.Month,
DateTime.DaysInMonth(selectedDate.Year, selectedDate.Month));
Since you never use the day value of your dattime picker, you can just set it to 1, which will always give an existing date.
This solution leaves me with a bad feeling somehow, since you are using a date that differs from the date you get from your control - always a possible source of errors, IMO. Remember to put comments into your code, explaining why you are doing it this way ;-)
I'd probably use the 1st of the month like Treb Selected, but extend DateTimePicker so that when you did this:
MyDateTimePicker.Value
it would do something like this:
get{
return value.addMonths(1).addDays(-1)
}

convert vb date comparison to c#

I have a code in vb, and I'm trying convert it to c#.
_nextContactDate.ToShortDateString > New Date(1900, 1, 1)
This is _nexContractDate declaration:
Private _nextContactDate As DateTime
It's weird for me. Comapre datetime to string?
What this code is doing is extracting the date part (i.e. removing the time part) and using VB's loose nature to allow a date represented as a string to be implicitly converted back to a date for the purposes of comparison with an actual date.
The correct way to remove the time part would be to check as follows:
_nextContactDate.Date > new DateTime(1900, 1, 1)
It seems odd, as this means that the 1st Jan 1900 will fail this check, and only dates from the 2nd Jan 1900 will pass. As such, I'd be inclined to check whether this code has a logic error.
I'm not sure I understand your question entirely, but why compare a DateTime to a string anyway, why not just compare dates?
if (_nextContactDate > new DateTime(1900, 1, 1))
{
}
As noted by Greg, currently the ToShortDateString removes some parts of the date (specifically, the time units), but upon comparison with a minimum date as such, this is rather redundant - if you are concerned at such a level, then you can compare only the Date members.
no, you don't need to compare DataTime variable in string format.
you can compare like below:
DateTime myDate = new DateTime(2011,8,24);
if(myDate > DateTime.MinValue)
DoSomething();
In many databases, time is stored as the minimum date + the time value.
So assuming the minimum date is 31 Dec 1899 2359H (if I reckon right, that's the minimum for Access) then 1300H will be stored as 01 Jan 1900 1300H.
Dates are stored as 'usual'. And dates with time components have the date value with the time component attached to them.
What's this got to do with the code? The original programmer is trying to determine whether the field is holding a date or a time value. The analogy is simple. If the value is time only, then by stripping off the time component, you'll be left with 01 Jan 1900. If it contains a date component, it's going to be more than 01 Jan 1900.

Categories