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

1 Answer

0 votes
#include <iostream>
#include <sys/time.h>
#include <cinttypes>

int64_t current_timestamp() {
    struct timeval tv;

    gettimeofday(&tv, NULL);

    int64_t milliseconds = (int64_t)tv.tv_sec * 1000L + (int64_t)tv.tv_usec / 1000;

    return milliseconds;
}

int main(void) {
    std::cout << current_timestamp();
}




/*
run:

1646820346492

*/

 



answered Mar 9, 2022 by avibootz

Related questions

...