How to set DateTime property of business objects RRS feed

  • Question

  • User197122282 posted

    I am using Business-Objects with DateTime properties in my application. For example, I have the following object:

    class EmployeeObject
         private int      employeeID;
         private DateTime dateJoined;
         public EmployeeObject () {}
         public int EmployeeID 
              get { return employeeID;}
              set { employeeID = value; }
         public DateTime DateJoined 
              get { return dateJoined;}
              set { dateJoined = value; }

    The problem is that whenever I try to assign a date value to any DateTime property, I receive the message "String was not recognized as a valid DateTime".

    EmployeeObject emp = new EmployeeObject ();
    emp.EmployeeID = 1;
    emp.DateJoined = Convert.ToDateTime (txtDate.Text); // here I get an error

    I have tried many variants such as:

    emp.DateJoined = DateTime.ParseExact (txtDate.Text, "MM-dd-yyyy", CultureInfo.InvariantCulture);
    emp.DateJoined = DateTime.ParseExact (txtDate.Text, "MM-dd-yyyy", new System.Globalization.CultureInfo ("en-US"));

    but none of the above works. I want to set the value in yyyy-MM-dd format which is accepted by SQL Server.

    Wednesday, September 4, 2013 5:50 AM

All replies

  • User441938976 posted
    string date = "01/08/2008";
    DateTime dt = Convert.ToDateTime(date);        

    Refer above code.

    Wednesday, September 4, 2013 6:02 AM
  • User-306750178 posted


    Try below code.

    string pattern = "MM-dd-yyyy";
    DateTime parsedDate;
    DateTime.TryParseExact(txtDate.Text, pattern, null, DateTimeStyles.None, out parsedDate)
    EmployeeObject emp = new EmployeeObject ();
    emp.EmployeeID = 1;
    emp.DateJoined = parsedDate;

    This will definately work for you.

    Wednesday, September 4, 2013 6:07 AM
  • User-1006641900 posted
    The answer to your problem will in first formatting the string to date format and then converting it to date time. If you want to try it yourself then go ahead this would solve the issue, or if you are looking for the specific answer (the code) then please post the date string that you get from txtDate.Text.
    Wednesday, September 4, 2013 6:12 AM
  • User197122282 posted
    ramu, I tried your example, but the value set is "1/1/0001 12:00:00 AM". Any ideas?
    Wednesday, September 4, 2013 6:42 AM
  • User-37275327 posted

    Problem seems is text on the text box is not valid to convert to datetime. Try following alternative

     DateTime testDate;
     DateTime.TryParse(txtDate.Text.Trim(), testDate);
     emp.DateJoined = testDate;

    Thursday, September 5, 2013 12:52 AM
  • User197122282 posted

    tried that but didn't work.

    Thursday, September 5, 2013 1:07 AM
  • User-37275327 posted

    Oops my bad, out missed, if the expression can not evaluate today will assigned to DateJoined .

    DateTime testDate=DateTime.Today;
    DateTime.TryParse(txtDate.Text.Trim(),out testDate);
    emp.DateJoined = testDate;

    Thursday, September 5, 2013 3:07 AM
  • User-488622176 posted

    When parsing a date (from String format) you'll need to consider the fact the string can be in a different display/localisation format. For example : the US notion of May 1st is 05/01/2013, where the European notion is 01/05/2013. Therefore, you should include the localisation parameter when parsing dates. Please check this article : http://stackoverflow.com/questions/1068386/parsing-datetime-on-localized-systems for sample code.

    Monday, September 9, 2013 7:22 AM
  • User753101303 posted


    And what is the value you have in txtDate.Text wehn it appears ?

    Monday, September 9, 2013 7:34 AM
  • User-1199946673 posted

    want to set the value in yyyy-MM-dd format which is accepted by SQL Server.

    You don't understand. When you enter the value of a Date Paramater as a string, than you need to worry about the format of the string. But in this case, you store the value as a date. A date is not stored in any particular format, a date is a date, only once you want to display it (for example in a webpage, you need to think aboutin which format you want to display the date.

    Here you need to think in which format the date should be entered in the TextBox by your users. You need to specify that format as the second parameter in tne ParseExact Method. Don't worry about the format of SQL. Once the DateJoined value is filled, you can pass that value as the parametervalue to your SQL statement.


    Monday, September 9, 2013 7:39 AM
  • User-488622176 posted

    A DateTime object is culture invariant, and contains a date value (underlying in UTC format). In order to convert text to a date, you use Parse or ParseExact. A text is always in a specific localisation (mostly client browser/OS locale). So you need to pass this info to the Parse or ParseExact methods in order to convert correctly.

    To display you need to convert the date back to text. This is done through the "ToString" method. Again you need to pass the cultureinfo to show the correctly formatted date for that culture.

    If you do not provide anything, it takes the defaults for the session. This can be the default of the server, or the client browser, ....  You do not actually know. This implies you can get nasty errors converting when you make certain assumptions.

    Monday, September 9, 2013 8:27 AM