I have an UNIX Timestamp in my Arduino Sketch and would like to get the day of the week of this timestamp (1-7).

How can I convert the timestamp to get the day of the week?

  • Do you mean, your program will be given a UNIX Timestamp number as data and should produce a day-of-the-week number? Or that you have a timestamp number and want to know its day-of-the-week number? If the latter, the Unix date command can tell you, via format code +%u. Eg, if date +%s says 1455407637 (for Sat Feb 13 16:53:57 MST 2016), date -d@1455407637 +%u says 6. – James Waldby - jwpat7 Feb 13 '16 at 23:58

The simplest solution to that problem is to download the Time library and use the weekday() function:

int weekday(time_t t); // the weekday for the given time 


  • 1
    One caveat, the breakTime() code at line 168 of Time.cpp computes Wday = (t/(60*60*24) + 4) % 7) + 1, ie, it apparently ignores leap seconds [26 in number since 1972] so for 26 seconds before every midnight the day-of-the-week calc will be off by 1. – James Waldby - jwpat7 Feb 14 '16 at 0:12
  • 2
    Ignore previous comment. As noted in wikipedia's Unix time article, Unix timestamps don't count leap seconds, so they are locally correct and calculations near midnight will be ok, except during a leap second itself. Also see Eric Raymond's Time, Clock, and Calendar Programming In C article, and the article Unix leap seconds. – James Waldby - jwpat7 Feb 14 '16 at 0:58
  • UNIX timestamps are also kept in GMT/UTC time. So you may run into problems with it being some hours out. If so look to the functions gmtime() Vs locatime(). You may need to set your time-zone. Oh and watch out for daylight savings time if it matters for your project. – Kingsley Feb 15 '16 at 3:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.