none
How to convert string to datetimepicker format? RRS feed

  • Question

  • Hi guys, I wanna convert date data from database to datetimepicker format. So, the date data (string) will be displayed with datetimepicker. I know how to change the datetimepicker format to string when I wanna save it to database but I cann't do the otherwise. 

    //This is the part of my codes which I got error on
    
    string date1 = DataView4.SelectedRows[0].Cells[4].Value.ToString();
    DateTime dt = DateTime.Parse(date1);
    DateTimePicker1.Value = dt;

    I've also tried this code, but it also didn't work...

    dateTimePicker1.Value = DateTime.ParseExact(
        date1, 
        "yyyy|MM|dd", 
        CultureInfo.InvariantCulture);

    also this code, but it gave same result... nothing works...

    dateTimePicker1.Value = DateTime.ParseExact(date1, "yyyy|MM|dd", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None);

    Saturday, December 19, 2015 9:39 AM

Answers

  • What it's looking for is a datetime.

    There are a number of  ways to translate a string to a datetime, including

    datetime.parse

    datetime.parseexact

    Convert.ToDateTime

    That makes me think your string isn't a valid date or it's in the wrong format.

    This works ok for me:

                string testDate = "2015|12|19";
                DateTime dt = DateTime.ParseExact(
        testDate, "yyyy|MM|dd",
        CultureInfo.InvariantCulture);

    Take a look at your string.

    When you have problems like this it's an idea to start up a test solution you can play around with.

    Just throw strings and stuff around hard coded.

    Put a break point in.

    Spin it up with an f5

    Step through and explore what's happening.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles


    • Edited by Andy ONeillModerator Saturday, December 19, 2015 9:52 AM
    • Marked as answer by The_C Sunday, December 20, 2015 10:25 AM
    Saturday, December 19, 2015 9:51 AM
    Moderator
  • You should store the values as dates in the database rather than storing them as strings. A database is used to store data, not to format it.

    If you want to convert a string to a DateTime you need to know exactly how the string looks like. What exact value does your "date1" string contains at runtime? Put a breakpoint on it and debug to find out. If it is for example "2015|12|19", this should work:

    string date1 = "2015|12|19";
    DateTime dt = DateTime.ParseExact(date1, "yyyy|MM|dd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
    But it depends on the value of the string.


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by The_C Sunday, December 20, 2015 10:25 AM
    Saturday, December 19, 2015 9:57 AM
  • It seems Magnus and I had pretty much the same idea and posted it at the same time.

    I would have thought you can actually see the string in your first row, fifth cell of your datagridview.

    .

    How about your issue now.

    If you need more help then could you explain:

    When you debug, what exactly is the string you get in date1?

    I suspect as you look at that then you will probably find your issue.

    But if you don't then come back with more info.

    .

    If you've resolved your issue please mark a post or posts as answer and consider upvoting those that helped.

    Thanks in advance.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    • Marked as answer by The_C Sunday, December 20, 2015 10:25 AM
    Saturday, December 19, 2015 1:00 PM
    Moderator

All replies

  • What it's looking for is a datetime.

    There are a number of  ways to translate a string to a datetime, including

    datetime.parse

    datetime.parseexact

    Convert.ToDateTime

    That makes me think your string isn't a valid date or it's in the wrong format.

    This works ok for me:

                string testDate = "2015|12|19";
                DateTime dt = DateTime.ParseExact(
        testDate, "yyyy|MM|dd",
        CultureInfo.InvariantCulture);

    Take a look at your string.

    When you have problems like this it's an idea to start up a test solution you can play around with.

    Just throw strings and stuff around hard coded.

    Put a break point in.

    Spin it up with an f5

    Step through and explore what's happening.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles


    • Edited by Andy ONeillModerator Saturday, December 19, 2015 9:52 AM
    • Marked as answer by The_C Sunday, December 20, 2015 10:25 AM
    Saturday, December 19, 2015 9:51 AM
    Moderator
  • You should store the values as dates in the database rather than storing them as strings. A database is used to store data, not to format it.

    If you want to convert a string to a DateTime you need to know exactly how the string looks like. What exact value does your "date1" string contains at runtime? Put a breakpoint on it and debug to find out. If it is for example "2015|12|19", this should work:

    string date1 = "2015|12|19";
    DateTime dt = DateTime.ParseExact(date1, "yyyy|MM|dd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
    But it depends on the value of the string.


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by The_C Sunday, December 20, 2015 10:25 AM
    Saturday, December 19, 2015 9:57 AM
  • It seems Magnus and I had pretty much the same idea and posted it at the same time.

    I would have thought you can actually see the string in your first row, fifth cell of your datagridview.

    .

    How about your issue now.

    If you need more help then could you explain:

    When you debug, what exactly is the string you get in date1?

    I suspect as you look at that then you will probably find your issue.

    But if you don't then come back with more info.

    .

    If you've resolved your issue please mark a post or posts as answer and consider upvoting those that helped.

    Thanks in advance.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    • Marked as answer by The_C Sunday, December 20, 2015 10:25 AM
    Saturday, December 19, 2015 1:00 PM
    Moderator
  • yes sir... you're right... the problem is about my string. It has different format with the datetimepicker format property. I just need to change my datetimepicker format to 'Custom' and then it works now... 

    Thanks in advance for your help.

    Sunday, December 20, 2015 10:30 AM
  • The problem is about my string format. I just need to change datetimepicker format to 'Custom' and then it works fine. I convert my data to string when I wanna store it do database because most of tutorials I learned from taught me that way. 

    Thanks in advance for your help sir....

    Sunday, December 20, 2015 10:34 AM