How to format DATETIME
-
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
-
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
-
You probably need to use the DateTimePicker.ToString Method (System.Windows.Forms) | Microsoft Docs[^] to format the date and time properly.
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 -
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 -
dateTimePicker2->Value = DateTime::Now.AddDays(1);
MessageBox::Show(dateTimePicker2.ToString()); // Needs pointer access characters "->", not "."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
-
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
-
Sorry, my mistake. You need to call ToString on the Value property like:
MessageBox::Show(dateTimePicker2->Value.ToString("MMMM dd, yyyy - dddd"));
Many , many thanks - your swift responses appreciated enormously Works fine
-
Many , many thanks - your swift responses appreciated enormously Works fine
-
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.
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
-
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
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.