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,870 questions

51,793 answers

573 users

How to get time in milliseconds since epoch in C++

2 Answers

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

int main() {
    auto milliseconds_since_epoch = std::chrono::duration_cast<std::chrono::milliseconds>(
                                    std::chrono::system_clock::now().time_since_epoch()).count();

    std::cout << "milliseconds since epoch: " << milliseconds_since_epoch; 
    
    return 0;
}
 
 
 
 
/*
run:
 
milliseconds since epoch: 1621175558972

*/

 



answered May 16, 2021 by avibootz
0 votes
#include <iostream>
#include <sys/time.h>

int main() {
    struct timeval time_now{};
    
    gettimeofday(&time_now, nullptr);

    time_t milliseconds = (time_now.tv_sec * 1000) + (time_now.tv_usec / 1000);

    std::cout << "milliseconds since epoch: "  << milliseconds;
    
    return 0;
}
 
 
 
 
/*
run:
 
milliseconds since epoch: 1621175940848

*/

 



answered May 16, 2021 by avibootz
...