1

I have been given the following code in C#:

  int Year, Month, Day, Hour, Min, Sec;
  ....
  //Assign Year, Month,..etc Value
  ....
  DateTime date = new DateTime(Year, Month, Day, Hour, Min, Sec);
  return date.ToString();

Could someone please suggest the C++ equivalent (for C++Builder XE)? The C# code converts a date and time according to the Local Region specified in the Windows OS. I want the same functionality in my C++ code. How can I achieve this?

All year, month, etc values are available in my C++ code.

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
  • 9
    did you take a look at std::chrono ? – Antoine Morrier Aug 16 '17 at 11:41
  • give a look of my question here: https://stackoverflow.com/questions/16647819/timegm-cross-platform or look for `gmtime`: http://www.cplusplus.com/reference/ctime/gmtime/ or for `mktime`: http://en.cppreference.com/w/cpp/chrono/c/mktime – Elvis Dukaj Aug 16 '17 at 12:24
  • @AntoineMorrier: std:chrono is for c++ 11 compiler.. I am using Borland C++ in RAD XE Builder IDE. – Pratik Mota Aug 16 '17 at 12:41
  • 2
    What about C++Builder's `System::TDateTime` and `Dateutils::EncodeDateTime()`? Ok, the latter has an extra argument for milliseconds, but otherwise, it does the same as the C# `DateTime` constructor. http://docwiki.embarcadero.com/Libraries/Seattle/en/System.DateUtils.EncodeDateTime – Rudy Velthuis Aug 16 '17 at 13:09
  • In addition to Rudy's suggestion, also look at the `Sysutils::DateTimeToStr()` function and other related formatting functions. – Remy Lebeau Aug 16 '17 at 19:32
  • @RudyVelthuis & ALL :: Thank you all.. I tried and it is wokring now... TDateTime dtTime = EncodeDateTime(nYear, nMonth, nDay, nHour, nMin, nSec, 0); UnicodeString sLocalTime = dtTime.DateTimeString(); – Pratik Mota Aug 18 '17 at 12:52

1 Answers1

0
#include <dateutils.hpp>

 TDateTime dtTime = EncodeDateTime(nYear, nMonth, nDay, nHour, nMin, nSec, 0);
 UnicodeString sLocalTime = dtTime.DateTimeString();
 AnsiString sTempString = sLocalTime.c_str();