none
DateTime RRS feed

  • Question

  • Hello , Iam working on WidowsForms and i have a textbox which takes user input of 13 digits. From these 13 digits I have to use the first 6 digits and display the as date in the format dd mmm yyyy, how do I display the output? here's my code:

                        

                string dat = txtID.Text.Substring(0, 6);
                DateTime pme =  DateTime.ParseExact(dat, "dd mmm yyyy", CultureInfo.CurrentCulture);
                MessageBox.Show(Convert.ToString(pme));

    Thanks.

    Friday, July 6, 2018 6:06 PM

Answers

  • DateTime.ParseExact is used to extract date and time fields from a string.  You need to do exactly the opposite.  The code you have above is going to expect the "dat" string to contain, for example, "06 Jul 2018".

    You have "mmm" with three characters.  Usually, when you have three characters, you're looking for a text month abbreviation, like "Jul".  If so, you want "MMM" with capital letters.

    We can't actually answer your question, because you didn't say what those 6 digits contain.  Is it, for example, "yymmdd"?  So, today would be "180706"?  If so, then this does more or less what you asked:

    {
        string d1 = "180706";
        int d1v = Int32.Parse(d1);
        int yy = d1v / 10000;
        int mm = (d1v / 100) % 100;
        int dd = d1v % 100;
        DateTime pme = new DateTime(2000+yy, mm, dd);
        MessageBox.Show( pme.ToString("dd MMM yyyy") );
    }



    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by Do2t Friday, July 6, 2018 10:01 PM
    Friday, July 6, 2018 6:17 PM

All replies

  • DateTime.ParseExact is used to extract date and time fields from a string.  You need to do exactly the opposite.  The code you have above is going to expect the "dat" string to contain, for example, "06 Jul 2018".

    You have "mmm" with three characters.  Usually, when you have three characters, you're looking for a text month abbreviation, like "Jul".  If so, you want "MMM" with capital letters.

    We can't actually answer your question, because you didn't say what those 6 digits contain.  Is it, for example, "yymmdd"?  So, today would be "180706"?  If so, then this does more or less what you asked:

    {
        string d1 = "180706";
        int d1v = Int32.Parse(d1);
        int yy = d1v / 10000;
        int mm = (d1v / 100) % 100;
        int dd = d1v % 100;
        DateTime pme = new DateTime(2000+yy, mm, dd);
        MessageBox.Show( pme.ToString("dd MMM yyyy") );
    }



    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by Do2t Friday, July 6, 2018 10:01 PM
    Friday, July 6, 2018 6:17 PM
  • you can use String.Remove() Method to separate. This is Example :

    string date = "180707";
    int Year = 2000 + int.Parse(date.Remove(2));
    int Month = int.Parse(date.Remove(0, 2).Remove(2));
    int Day = int.Parse(date.Remove(0, 4));
    DateTime pme = new DateTime(Year, Month, Day);
    MessageBox.Show(pme.ToString("dd MMM yyyy"));


    • Proposed as answer by Aryan Mesgari Saturday, July 7, 2018 8:50 AM
    • Unproposed as answer by Aryan Mesgari Saturday, July 7, 2018 8:50 AM
    • Edited by Aryan Mesgari Saturday, July 7, 2018 8:56 AM
    • Proposed as answer by Aryan Mesgari Saturday, July 7, 2018 12:27 PM
    • Unproposed as answer by Aryan Mesgari Sunday, July 8, 2018 12:48 PM
    Saturday, July 7, 2018 8:50 AM
  • Aryan ... while the code in your reply works just fine, you should never propose your own reply as an answer. It is not proper Forum ettiquette to do so. I'll leave it marked as such, because as I said it *does* work and I would have Proposed it myself. But please, in the future, do not mark your own replies as an answer or as a proposed answer.

    Thanks ...


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, July 7, 2018 4:34 PM
    Moderator
  • I'm idly curious to know why you would choose the Remove method instead of Substring?  It's true your code works, but I find Remove to be confusing; it implies that something is being removed from the string, when of course it isn't.  Isn't this:

        int Month = int.Parse(date.Substring(2,2));

    more obvious on first reading than this:

        int Month = int.Parse(date.Remove(0,2).Remove(2));

    And I fully acknowledge this is not important...


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Tuesday, July 10, 2018 12:26 AM
  • Tim --- I guess it's just a matter of personal preference. I agree with you that Substring() is a heckuva lot more obvious than Remove(), but perhaps Aryan is used to using Remove() and is why he suggested it.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Tuesday, July 10, 2018 4:57 AM
    Moderator