Calculate week number & day number

ESP32 - Wifi
Android version 11
Blynk server
v 0.6.1

Hi, i’m using RTCwidget for getting the Blynk server time.
In my project i need to calculate the week number (week of year) and the day number (day of week starting from monday).
Is there an easy way to do this? Are there variables in the RTC that i can use?

The Time library does all of this for you.
Just be aware that if you use the Blynk time input widget then it uses Monday as day 1, whereas the Time library uses Sunday as day 1.

The Sketch Builder time input examples are a good starting point, as are the Time library examples.

Also, be aware that I think Blynk uses UINX time in milliseconds and the Time library might default to seconds (since 1st Jan 1970), so you may need to divide the Blynk time value by 1000


Hi Pete, thanks for the quick answer.

Could you be a bit more specific?

In timelib i could detect the weekday() function that returns the weeknumber. This solves half of my problem.

I browsed the timelib.h and time.h libraries and i could not find a function that gives me a week number based on a time/date (retrieved from RTC).
I also, as per your suggestion, looked into the Blynk example builder to the Time Input (simle/advanced), but no luck there. Or did you mean something else?

This might help

I think there’s a variable called %U in Time that returns the week number.


I thought i was getting closer to the answer, but still 'im missing something.
Does anybody see the mistake?
I’m getting 00 as week number (in both functions)

#include "time.h"
#include <WidgetRTC.h>

  terminal.print("CONNECTED to server" );
  //Sync time on connection
  String currentime= String(hour())+":"+minute()+":"+second();
  terminal.print(currentime + " Blynk Server Time Sync'ed");
  int test;
  terminal.println("weeknbr"+ test);

void getWeek(int& week) {
  time_t rawtime;
  struct tm * timeinfo;
  char buffer[4];

  timeinfo = localtime(&rawtime);

  strftime(buffer,4,"%W",timeinfo);   // '%W' = week number of the year, eg 1/1/09 == 1

  week = atoi(buffer);     // convert char to int

  terminal.print("Week number is ");
  terminal.println(String(week)); //Prints out week number
String Weekday = dayAsString(weekday());

Hi Blynk_Coeur,
thanks for your suggestion, but the purpose is to get the week number in the year. So, for today (4/12/2021) the result should be week ‘48’.

Sorry , I didn’t read well :wink:

Try this

int main ()
  time_t rawtime;
  struct tm * timeinfo;
  char buffer [80];

  time (&rawtime);
  timeinfo = localtime (&rawtime);

  strftime (buffer,80,"%U",timeinfo);
  puts (buffer);

  return 0;

Hi John93, unfortunately this didn’t work out. Same result: week 00

Detailed debugging made me come to the conlusion that the localtime function (in timeinfo = localtime (&rawtime);) always gave me 1 jan1970. So, this means that rtc.begin() doesn’t affect the localtime.
I could solve it by adding:

timeinfo->tm_year = year() - 1900;
  timeinfo->tm_mon = month() - 1;
  timeinfo->tm_mday = day();
  mktime ( timeinfo );

This now does the job fine for me. I don’t know whether this is the most efficient, but it suits the purpose for me :wink: