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

1 Answer

0 votes
#include <stdio.h>
#include <sys/time.h>
#include <inttypes.h>

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) {
    printf("%" PRId64 "\n", current_timestamp());
}




/*
run:

1646820139391

*/

 



answered Mar 8, 2022 by avibootz
edited Mar 9, 2022 by avibootz

Related questions

...