Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Odd date behavior
Message
 
 
To
11/04/2018 11:57:50
General information
Forum:
C#
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01659301
Message ID:
01659314
Views:
40
Why you're using 2 digits format for the year? Use 4 digits to avoid ambiguity as Thomas said.

>>What exactly you're using? Is it WinForm?
>>
>>BTW, your comments don't match what methods do. I would fix this first.
>>
>
>Naomi
>Yes, it's winform.
>
>>>This code has been running for a couple of years and a bug has surfaced.
>>>
>>>If a date is 12/31/2099 and the user changes the year to 39 (the user can't see the century)
>>>the code changes the year to 1939.
>>>
>>>On the other hand, if the user changes the year 29, the date becomes 2029.
>>>
>>>After googling and fiddling for a few hours, I punted and changed the control to a datetimepicker, but I'd still like to know what's happening.
>>>
>>>Here's the code that sets up the control
>>>
>>>Any ideas?
>>>
>>>
>>>
>>>
>>>  private void SetTextBoxDatetimeBinding(TextBox txtbox,  DataTable dt, string fieldname)
>>>    {
>>>
>>>        Binding b = new Binding("Text", dt, fieldname);
>>>        {
>>>            b.Format += new ConvertEventHandler(DatetimeToString);
>>>            b.Parse += new ConvertEventHandler(DateTimeStringToDateTime);
>>>        }
>>>        txtbox.DataBindings.Clear();
>>>        txtbox.DataBindings.Add(b);
>>>
>>>    }
>>>
>>>    private void DatetimeToString(object sender, ConvertEventArgs cevent)
>>>    {
>>>        // The method converts only to string type. Test this using the DesiredType.
>>>        if (cevent.DesiredType != typeof(string)) return;
>>>
>>>        // Use the ToString method to format the value as currency ("c").
>>>        cevent.Value = ((DateTime)cevent.Value).ToString("MM/dd/yy");
>>>    }
>>>
>>>
>>>    private void DateTimeStringToDateTime(object sender, ConvertEventArgs cevent)
>>>    {
>>>        // The method converts back to decimal type only. 
>>>        if (cevent.DesiredType != typeof(DateTime)) return;
>>>
>>>        // Converts the string back to decimal using the static Parse method.
>>>        cevent.Value = DateTime.Parse(cevent.Value.ToString());
>>>    }
>>>
>>>  
>>>
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform