0
#include <time.h>
#include <stdio.h>

int main()
{
  time_t mytime;
  mytime = time(NULL);
  printf(ctime(&mytime));
  return 0;
}

The above program prints the current date and time as follows: enter image description here

Tue Apr 12 00:37:48 2022

Is there a way to print the current date and time down to the millisecond? So the output would look like this instead:

Tue Apr 12 00:37:48.234567 2022
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
User234567
  • 93
  • 7
  • 1
    See [Custom date and time including nanoseconds](https://stackoverflow.com/q/71809882/15168), and the [comment](https://stackoverflow.com/questions/71809882/custom-date-and-time-including-nanoseconds-in-c/71809972#comment126900748_71809972) for how to print microseconds and milliseconds. You may have other choices for the subsecond timing functions (`gettimeofday()` reports to microseconds; `ftime()` reports to milliseconds), but the basic ideas are the same. Use `clock_gettime()` if you're on POSIX or `timespec_get()` if you have C11/C18 available; only use the alternatives if they're AWOL. – Jonathan Leffler Apr 12 '22 at 00:45
  • Your example output seems to be printing microseconds rather than milliseconds as the question title and body suggest. – Jonathan Leffler Apr 12 '22 at 00:46
  • You shouldn't really post images of plain text — just show the plain text in the question. – Jonathan Leffler Apr 12 '22 at 00:50

0 Answers0