Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. How to format DATETIME

How to format DATETIME

Scheduled Pinned Locked Moved Managed C++/CLI
c++csharpvisual-studiohelptutorial
10 Posts 2 Posters 26 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 14124661
    wrote on last edited by
    #1

    I am trying to build some simple examples as part of my learning process. I have created a form which includes the dateTimePicker and tried several combinations which have failed. The output initially displays time as HH:MM:00 and only dis[plays correct time when I include ...AddHours(0)... I'd like to format - say

    "MMMM dd, yyyy - dddd"

    My standard output is

    20/02/2019 00:00:00

    private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
    String ^myText = "1&2-END";
    dateTimePicker2->CustomFormat = "MMMM dd, yyyy - dddd";
    dateTimePicker2->Value = DateTime::Now.AddDays(+30).Date;//dateTimePicker2->Value = DateTime::Today;
    myText = myText + "\r\n" + dateTimePicker2->Value;
    dateTimePicker2->Value = DateTime::Now.AddDays(+30).AddHours(0);//dateTimePicker2->Value = DateTime::Today;
    myText = myText + "\r\n" + dateTimePicker2->Value;
    //myText = myText->Format('YYYY-MM-DD');
    myText= myText + "\r\n" + "& 3 -END";
    //dateTimePicker2.DateTimePickerFormat -> "MMMM dd, yyyy - dddd";
    //dateTimePicker2.DateTimePickerFormat.Custom;;
    myTB2->Text = myText;
    }

    I am using Visual Studio 2017, C++/CLi, W10 Professional Guess I am missing some declaration - I have in .cpp

    #include "MyFirstForm.h"
    #include
    #include
    #include

    //#include _
    using namespace System;
    using namespace System::Windows::Forms;
    using namespace System::IO;
    [STAThreadAttribute]_

    Any help appreciated. Looks like great way to develop FORM & C++ based programs

    L 1 Reply Last reply
    0
    • M Member 14124661

      I am trying to build some simple examples as part of my learning process. I have created a form which includes the dateTimePicker and tried several combinations which have failed. The output initially displays time as HH:MM:00 and only dis[plays correct time when I include ...AddHours(0)... I'd like to format - say

      "MMMM dd, yyyy - dddd"

      My standard output is

      20/02/2019 00:00:00

      private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
      String ^myText = "1&2-END";
      dateTimePicker2->CustomFormat = "MMMM dd, yyyy - dddd";
      dateTimePicker2->Value = DateTime::Now.AddDays(+30).Date;//dateTimePicker2->Value = DateTime::Today;
      myText = myText + "\r\n" + dateTimePicker2->Value;
      dateTimePicker2->Value = DateTime::Now.AddDays(+30).AddHours(0);//dateTimePicker2->Value = DateTime::Today;
      myText = myText + "\r\n" + dateTimePicker2->Value;
      //myText = myText->Format('YYYY-MM-DD');
      myText= myText + "\r\n" + "& 3 -END";
      //dateTimePicker2.DateTimePickerFormat -> "MMMM dd, yyyy - dddd";
      //dateTimePicker2.DateTimePickerFormat.Custom;;
      myTB2->Text = myText;
      }

      I am using Visual Studio 2017, C++/CLi, W10 Professional Guess I am missing some declaration - I have in .cpp

      #include "MyFirstForm.h"
      #include
      #include
      #include

      //#include _
      using namespace System;
      using namespace System::Windows::Forms;
      using namespace System::IO;
      [STAThreadAttribute]_

      Any help appreciated. Looks like great way to develop FORM & C++ based programs

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You probably need to use the DateTimePicker.ToString Method (System.Windows.Forms) | Microsoft Docs[^] to format the date and time properly.

      M 1 Reply Last reply
      0
      • L Lost User

        You probably need to use the DateTimePicker.ToString Method (System.Windows.Forms) | Microsoft Docs[^] to format the date and time properly.

        M Offline
        M Offline
        Member 14124661
        wrote on last edited by
        #3

        Hi & many thanks for your response....however I added the following:-

        dateTimePicker2->Value = DateTime::Now.AddDays(1);
        MessageBox::Show(dateTimePicker2.ToString());
        myTB2->Text = myText;
        

        and failed to complie with the following error - is this what you meant. When I follow your link it refers to C# whereas I am working with C##.

        Severity Code Description Project File Line Suppression State
        Error C2228 left of '.ToString' must have class/struct/union Project1 c:\users\home\documents\visual studio 2017\projects\project1\project1\myfirstform.h 335
        Error (active) E0153 expression must have class type Project1 C:\Users\HOME\Documents\Visual Studio 2017\Projects\Project1\Project1\MyFirstForm.h 336

        L 1 Reply Last reply
        0
        • M Member 14124661

          Hi & many thanks for your response....however I added the following:-

          dateTimePicker2->Value = DateTime::Now.AddDays(1);
          MessageBox::Show(dateTimePicker2.ToString());
          myTB2->Text = myText;
          

          and failed to complie with the following error - is this what you meant. When I follow your link it refers to C# whereas I am working with C##.

          Severity Code Description Project File Line Suppression State
          Error C2228 left of '.ToString' must have class/struct/union Project1 c:\users\home\documents\visual studio 2017\projects\project1\project1\myfirstform.h 335
          Error (active) E0153 expression must have class type Project1 C:\Users\HOME\Documents\Visual Studio 2017\Projects\Project1\Project1\MyFirstForm.h 336

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          dateTimePicker2->Value = DateTime::Now.AddDays(1);
          MessageBox::Show(dateTimePicker2.ToString()); // Needs pointer access characters "->", not "."

          M 1 Reply Last reply
          0
          • L Lost User

            dateTimePicker2->Value = DateTime::Now.AddDays(1);
            MessageBox::Show(dateTimePicker2.ToString()); // Needs pointer access characters "->", not "."

            M Offline
            M Offline
            Member 14124661
            wrote on last edited by
            #5

            Thanks again, Apologies - but really struggling with this one

            MessageBox::Show(dateTimePicker2->ToString());
            MessageBox::Show(dateTimePicker2->ToString(), "MMMM dd, yyyy - dddd");

            Both lines of code show same results -

            System.Windows.Forms.DateTimePicker, Value: 22/01/2019 14:26:18

            whereas I'd just like to see January 22, 2019 and in this case no time

            L 1 Reply Last reply
            0
            • M Member 14124661

              Thanks again, Apologies - but really struggling with this one

              MessageBox::Show(dateTimePicker2->ToString());
              MessageBox::Show(dateTimePicker2->ToString(), "MMMM dd, yyyy - dddd");

              Both lines of code show same results -

              System.Windows.Forms.DateTimePicker, Value: 22/01/2019 14:26:18

              whereas I'd just like to see January 22, 2019 and in this case no time

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Sorry, my mistake. You need to call ToString on the Value property like:

              MessageBox::Show(dateTimePicker2->Value.ToString("MMMM dd, yyyy - dddd"));

              M 1 Reply Last reply
              0
              • L Lost User

                Sorry, my mistake. You need to call ToString on the Value property like:

                MessageBox::Show(dateTimePicker2->Value.ToString("MMMM dd, yyyy - dddd"));

                M Offline
                M Offline
                Member 14124661
                wrote on last edited by
                #7

                Many , many thanks - your swift responses appreciated enormously Works fine

                L 1 Reply Last reply
                0
                • M Member 14124661

                  Many , many thanks - your swift responses appreciated enormously Works fine

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Happy to help. I don't know what your background is, but if you are just starting out you might find C# a better choice than C++/CLI.

                  M 1 Reply Last reply
                  0
                  • L Lost User

                    Happy to help. I don't know what your background is, but if you are just starting out you might find C# a better choice than C++/CLI.

                    M Offline
                    M Offline
                    Member 14124661
                    wrote on last edited by
                    #9

                    Many thanks. Very mixed background Wrote first programs in 60's (machine code, user code, Fortran, Algol & many more) Moved into IT sales / management Have developed Forex applications in MT4 which is sort of C based Started with this C++ / CLi on Saturday - so sort of making progress I've fought shy of 'understanding' the ins / outs of C Now retired and need to keep brain cells stimulated ! Thanks for support & comments and will read around

                    L 1 Reply Last reply
                    0
                    • M Member 14124661

                      Many thanks. Very mixed background Wrote first programs in 60's (machine code, user code, Fortran, Algol & many more) Moved into IT sales / management Have developed Forex applications in MT4 which is sort of C based Started with this C++ / CLi on Saturday - so sort of making progress I've fought shy of 'understanding' the ins / outs of C Now retired and need to keep brain cells stimulated ! Thanks for support & comments and will read around

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Since you have only just started with C++/CLI, my advice would be to stop now and switch to C#. It is so much better for this sort of application, and the learning curve is not as bad as it may at first appear. There is a great free starter document at .NET Book Zero[^] which you can work through fairly quickly. Like you, I am retired and followed this route myself, so I am far from being a C# expert, but I can code simple applications without too much trouble.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups