Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 answers

573 users

How to get the current date and time in C++

3 Answers

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


int main() {
    auto startd = std::chrono::system_clock::now();
    auto endd = std::chrono::system_clock::now();

    std::chrono::duration<double> elapsed_seconds = endd - startd;
    std::time_t end_time = std::chrono::system_clock::to_time_t(endd);

    std::cout << std::ctime(&end_time);
}


/*
run:

Fri Jun 12 15:23:31 2020

*/

 



answered Jun 12, 2020 by avibootz
0 votes
#include <iostream>
#include <ctime>  

int main() {
    time_t now = time(0);

    char *dt = ctime(&now);

    std::cout << dt << "\n";
}



/*
run:

Fri Jun 12 17:00:31 2020

*/

 



answered Jun 12, 2020 by avibootz
0 votes
#include <iostream>
#include <ctime>  

int main() {
   time_t now = time(0);

   tm *ltm = localtime(&now);

   std::cout << 1900 + ltm->tm_year << "-" << 1 + ltm->tm_mon << "-" << ltm->tm_mday;
   std::cout << " " << 1 + ltm->tm_hour << ":";
   std::cout << 1 + ltm->tm_min << ":";
   std::cout << 1 + ltm->tm_sec << "\n";
}



/*
run:

2020-6-12 18:11:9

*/

 



answered Jun 12, 2020 by avibootz
...