Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Odd date behavior
Message
De
11/04/2018 13:57:49
 
 
Information générale
Forum:
C#
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01659301
Message ID:
01659315
Vues:
39
Agree with you there. You'd think after the whole Y2K business not too long ago, that you'd want to avoid the problems of using 2-digit years. The frustrating part about century rollover is that sometimes the rollover occurs too soon, and sometimes too late - often depending on context of the date.

>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());
>>>>    }
>>>>
>>>>  
>>>>
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform