Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Odd date behavior
Message
De
12/04/2018 03:34:45
 
 
À
10/04/2018 19:15:16
Information générale
Forum:
C#
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01659301
Message ID:
01659332
Vues:
40
>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());
>    }
>
>  
>
See:

https://msdn.microsoft.com/en-us/library/system.globalization.calendar.twodigityearmax.aspx

and

https://msdn.microsoft.com/en-us/library/system.globalization.calendar.tofourdigityear(v=vs.110).aspx
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform