Convert a two digit year to a four digit year

前端 未结 17 966
误落风尘
误落风尘 2020-12-25 12:13

This is a question of best practices. I have a utility that takes in a two digit year as a string and I need to convert it to a four digit year as a string. right now I do <

17条回答
  •  旧巷少年郎
    2020-12-25 12:49

    Try this simple code

    //Invoke TextBoxDateFormat method with date as parameter.

    Method

    public void TextBoxDateFormat(string str1)
    
    {
    
    // Takes the current date format if MM/DD/YY or MM/DD/YYYY
    
    DateTime dt = Convert.ToDateTime(str1);
    
    //Converts the requested date into MM/DD/YYYY and assign it to textbox field
    
    TextBox = String.Format("{0:MM/dd/yyyy}", dt.ToShortDateString());
    
    
    //include your validation code if required
    
    }
    

提交回复
热议问题