Thread: How to get weekday?

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    2

    Question How to get weekday?

    I'm new in C and I want to know how to get the current weekday where sunday=0, monday=1,...,.
    (using time.h if possible)

    I want to do something like
    Code:
    #include<stdio.h>
    #include<time.h>
    
    int main(void)
    {
    
    int week_day;
    
    if(week_day==0) //if sunday 
    
    {
    do something;
    }
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Take a look here: <ctime> (time.h) - C++ Reference. You will probably be most interested in the time function and the gmtime or localtime functions (or their _r variants). For C, you will want to #include <time.h>. ctype is the C++ version of the header.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    The integer value you want is in the tm_wday member of struct tm. Here is a snippet to demonstrate this:

    Code:
    time_t t = time(NULL);
    struct tm *now = localtime(&t);
    printf("%d\n", now->tm_wday);
    See references for time and localtime for the details

    Man Page for time (freebsd Section 3) - The UNIX and Linux Forums
    Man Page for localtime (freebsd Section 3) - The UNIX and Linux Forums

  4. #4
    Registered User
    Join Date
    Aug 2014
    Posts
    2
    Thanks! Just what I was looking for. Nice blog and youtube channel btw. Thanks again!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing weekday
    By sworc66 in forum C Programming
    Replies: 12
    Last Post: 09-13-2002, 07:03 AM

Tags for this Thread