1

Is there any way to split the date and time from the System::DateTime?

Now you get something like: 2012-03-01 16:12:555

Is there any to separate them?

Ry-
  • 218,210
  • 55
  • 464
  • 476
Miracle
  • 11
  • 1
  • 6
  • Be sure to use the MSDN Library effectively, you won't get far without being able to look up the members of a class. When you do, you'll have no trouble finding the Date property to get the date and the TimeOfDay property to get the time. – Hans Passant Mar 10 '12 at 15:26

3 Answers3

2

If you have a DateTime and you want to split it into date and time, you can use the properties Date and TimeOfDay:

DateTime now = DateTime::Now;
DateTime date = now.Date;
TimeSpan time = now.TimeOfDay;

But if you only want to get the string representation of the date and time, you don't need those properties. You can use ToString() along with format strings:

String^ dateString = now.ToString("d");
String^ timeString = now.ToString("T");

One things you should remember about formatting in .Net is that it depends on the current culture. That means the result of calling ToString() will be formatted differently on a computer with Czech culture than on a computer with American culture.

svick
  • 236,525
  • 50
  • 385
  • 514
  • @svick When i add using namespace System or using System it's show me error on System , i am using visual c++ VS 2010 – AHF Nov 19 '13 at 15:20
  • @Ahmad I don't see how is that relevant to this question, but you probably [don't have C++/CLI enabled](http://stackoverflow.com/a/4969389/41071). Next time, when you have a question, [post a new question](http://stackoverflow.com/questions/ask), that's what this site is for. – svick Nov 19 '13 at 15:23
1

Simply create DateTime object like

Datetime test = DateTime::Now;

Then format it like this

String^ timestamp = test.ToString ("yyyy-MM-dd H:m:s");

This format is compatible with MySQL.

gnuanu
  • 2,252
  • 3
  • 29
  • 42
0

Well for date you can use Today
For the time you can use TimeOfDay

rerun
  • 25,014
  • 6
  • 48
  • 78
  • I got 6 errors: Can't show them ... Need to learn bb codes, will edit soon – Miracle Mar 10 '12 at 15:46
  • String^ GrabTime() { System::DateTime::Today CurDate = System::DateTime::Today; // Get Current Time. System::DateTime::TimeOfDay CurTime = System::DateTime::TimeOfDay; return CurDate.ToString();//Date return CurTime.ToString();//Time } – Miracle Mar 10 '12 at 15:52