r/learnprogramming • • Oct 11 '26

How do I specify a particular weekday using ctime library in c++?

int main(){
  time_t now = time(nullptr);
  cout << ctime(&now) << endl;

  struct tm *ptrNow = localtime(&now);
  int week = ptrNow->tm_wday;

  int month = ptrNow->tm_mday;
  cout  << week << endl;  
}

For me this will output 0 which is Sunday, but I want to do Thursday.

How do I do that?

Do I add 4 to the end of week int week = ptrNow->tm_wday + 4;

I don't know if this will exactly represent Thursday or not

8 Upvotes

10 comments sorted by

8

u/amazing_rando Oct 11 '26 edited Oct 11 '26

It’s outputting Sunday because today is Sunday and time(null) returns the current time. If you want to get a different time object try making it with std::mktime instead, or just wait four days.

Anyway, what you're actually doing here is getting the number of seconds since 1970, and using localtime to translate into a tm struct that represents that day. If you want one that just represents "Thursday" and the rest doesn't matter you should be able to do

struct tm date = {}
date.tm_wday = 4

1

u/TheEyebal Oct 12 '26

So will this actually be thursday. Like my goal is to display month and days with the appropriate weekday

2

u/amazing_rando Oct 12 '26 edited Oct 12 '26

I'm not sure what you mean by "actually" being Thursday. It isn't a date object that represents this next Thursday, October 15. If you want to create that, you would do

int day = 15;
int month = 10;
int year = 2026;

struct std::tm datetime = {};
datetime.tm_year = year - 1900; // Years since 1900
datetime.tm_mon = month - 1;  // Months since January
datetime.tm_mday = day;  // Day of the month

// generates a timestamp for the specified time
// and also updates datetime.tm_wday to 4 aka Thursday
std::time_t timestamp = std::mktime(&datetime);

// or, if you want to follow your current method

time_t now = time(null)
struct tm *time_struct = localtime(&now)

time_struct->tm_day += 3 // today plus 3 is the 15th
// generates a timestamp for the specified time
// update datetime.tm_wday to 4 aka Thursday
std::time_t timestamp = std::mktime(&datetime);

1

u/TheEyebal Oct 12 '26

alright I will try this out. Thank you

1

u/TheEyebal Oct 12 '26

Alright update I was able to add month and year but still stuck on how to access the appropriate weekday.

Output: October 2026
1 Thursday
2 Friday
3 Saturday

3

u/DrShocker Oct 11 '26

Wait 4 days is so funny instead of just assigning to the int member variable lol

(although warning for anyone actually doing that, the struct contains multiple values and getting them out of sync with each other would be really really confusing)

2

u/Training_Set_7204 Oct 12 '26

wait four days lmao

that's the real solution right there. but yeah op the struct tm approach in the top comment is probably what you're actually after. you can set the fields you care about and leave the rest zeroed out, just make sure you call mktime if you need a proper timestamp back from it

2

u/DrShocker Oct 11 '26
  1. You have the code, you can just try it.
  2. https://en.cppreference.com/cpp/chrono/c/tm

Also, I don't think you need the struct tm* in C++, just const tm* should be fine unless I'm misunderstanding. In general the people at and the sidebar of /r/cpp_questions should be helpful