How to get localtime in C++

2 Answers

0 votes
#include <iostream>
#include <ctime>

using std::cout;
using std::endl;

int main()
{
	time_t t = time(NULL);   
	tm* now = std::localtime(&t);

	cout << now->tm_mon + 1 << '/' << now->tm_mday << "/" << now->tm_year + 1900 << endl;
	cout << now->tm_sec << ':' << now->tm_min << ":" << now->tm_hour << endl;

	return 0;
}


/*
run:

6/2/2018
6:53:18

*/

 



answered Jun 2, 2018 by avibootz
0 votes
#include <iostream>
#include <ctime>

using std::cout;
using std::endl;

int main()
{
	time_t tm;
	struct tm *p;

	time(&tm);
	p = localtime(&tm);

	cout << asctime(p) << endl;
	
	return 0;
}


/*
run:

Sat Jun  2 18:55:43 2018

*/

 



answered Jun 2, 2018 by avibootz

Related questions

2 answers 178 views
178 views asked Jun 2, 2018 by avibootz
1 answer 182 views
...