简体   繁体   中英

How to Find a current day in c language?

I am able to get the current date but the output is like 9/1/2010,but my requirement is to get the current day like"Wednesday" not in form of integer value like 1. My code is here.

#include <dos.h>
#include <stdio.h>
#include<conio.h>

int main(void)
{
struct date d;
getdate(&d);
printf("The current year is: %d\n", d.da_year);
printf("The current day is: %d\n", d.da_day);
printf("The current month is: %d\n", d.da_mon);
getch();
return 0;

}

Please help me to find the current day as Sunday,Monday......... Thanks

Are you really writing for 16-bit DOS, or just using some weird outdated tutorial?

strftime is available in any modern C library:

#include <time.h>
#include <stdio.h>

int main(void) {
    char buffer[32];
    struct tm *ts;
    size_t last;
    time_t timestamp = time(NULL);

    ts   = localtime(&timestamp);
    last = strftime(buffer, 32, "%A", ts);
    buffer[last] = '\0';

    printf("%s\n", buffer);
    return 0;
}

http://ideone.com/DYSyT

The headers you are using are nonstandard. Use functions from the standard:

#include <time.h>

struct tm *localtime_r(const time_t *timep, struct tm *result);

After you call the function above, you can get the weekday from:

tm->tm_wday

Check out this tutorial/example .

There's more documentation with examples here .

As others have pointed out, you can use strftime to get the weekday name once you have a tm . There's a good example here :

   #include <time.h>
   #include <stdio.h>
   #include <stdlib.h>
   int
   main(int argc, char *argv[])
   {
       char outstr[200];
       time_t t;
       struct tm *tmp;

       t = time(NULL);
       tmp = localtime(&t);
       if (tmp == NULL) {
           perror("localtime");
           exit(EXIT_FAILURE);
       }

       if (strftime(outstr, sizeof(outstr), "%A", tmp) == 0) {
           fprintf(stderr, "strftime returned 0");
           exit(EXIT_FAILURE);
       }

       printf("Result string is \"%s\"\n", outstr);
       exit(EXIT_SUCCESS);
   }

Alternatively, if you insist on using your outdated compiler, there's a dosdate_t struct in <dos.h> :

struct dosdate_t {
  unsigned char  day;       /* 1-31          */
  unsigned char  month;     /* 1-12          */
  unsigned short year;      /* 1980-2099     */
  unsigned char  dayofweek; /* 0-6, 0=Sunday */
};

You fill it with:

void _dos_getdate(struct dosdate_t *date);

使用struct tm 示例

strftime is certainly the right way to go. Of course you could do

char * weekday[] = { "Sunday", "Monday",
                       "Tuesday", "Wednesday",
                       "Thursday", "Friday", "Saturday"};
char *day = weekday[d.da_day];

I'm of course assuming that the value getdate() puts in the date struct is 0-indexed, with Sunday as the first day of the week. (I don't have a DOS box to test on.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM