Date: 2021dec30
Date: 2025jul13
Language: C/C++
Q. C/C++: Get the Julian date | get the week of the year
A. The Julian date (Nth day of the year) is in the `tm struct':
#include <time.h>
int JulianDate() {
const time_t now = time(NULL);
struct tm here;
localtime_r(&now, &here);
return here.tm_yday;
}
The week of the year is not in there but you can use strftime():
int WeekOfYear() {
const time_t now = time(NULL);
struct tm here;
char buf[100];
localtime_r(&now, &here);
strftime(buf, sizeof(buf), "%U", &here);
// Or you may prefer %V - see `man strftime`
return atoi(buf);
}
Example Use:
#include <stdio.h>
void exampleUse() {
printf("julianDate=%d\n", julianDate());
printf("weekOfYear=%d\n", weekOfYear());
}