Convert string with datetime value to one with date and time in C/C++ on Visual Studio

I am developing a C++ application on Visual Studio in which I have a string that contains a datetime value and I must convert it into another string with the date and time in the format dd-mm-yyyy HH:MM:SS

For example, if the string sDateTime has the value "1643873040", I must get another string sDateAndTime with the value "02/03/2022, 08:24:00"

I have not found any tutorials or forums that explain how to do this conversion in a C or C++ program developed with Visual Studio and I would appreciate any help or suggestions.
You're referring to unix time, the seconds since epoch time. Maybe something using suggestions from https://stackoverflow.com/questions/13536886/unixtime-to-readable-date

Edit: Better example at
https://stackoverflow.com/questions/21589570/how-to-convert-a-time-t-type-to-string-in-c
Last edited on
That looks like a “unix time” — seconds elapsed since 1970/01/01. You can use the stuff in <ctime> or in <chrono> for it, or you can get Howard Hinnant’s “date” library for C++ (which I recommend).

I have to go now, so no examples. (Sorry)
http://www.cplusplus.com/reference/ctime/strftime/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <ctime>   

int main()
{
   char sDateAndTime[80];

   time_t t;
// time( &t );
   t = 1643873040;

   tm *timeinfo = localtime( &t );
   strftime( sDateAndTime, 80, "%d/%m/%Y, %H:%M:%S", timeinfo );

   std::cout << sDateAndTime;
}

Last edited on
Topic archived. No new replies allowed.