I want to save the date of today in a string and have the following code with the following output:
Code:
#include <stdio.h>
#include <time.h>
int main()
{
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf("%02d-%02d", tm.tm_mday, tm.tm_mon + 1);
printf("\n");
}
Output (for today, November the 5th):
05-11
What is the easiest way to save 05-11 in a string?
You can use the standard strftime function (declared in "time.h"), which takes a variety of format specifiers (akin to those used in printf) to extract and format the various date and/or time fields of a struct tm.
In your case, you would use the %d and %m specifiers to get the (two-digit) day and month numbers:
#include <stdio.h>
#include <time.h>
int main()
{
char StrDate[8];
time_t t = time(NULL);
strftime(StrDate, sizeof(StrDate), "%d-%m", localtime(&t));
printf("%s\n", StrDate);
return 0;
}
You could even add the newline character to the StrDate buffer directly, by appending the %n format specifier.
I used sprintf() which worked fine for me
#include <stdio.h>
#include <time.h>
int main()
{
time_t t = time(NULL);
struct tm tm = *localtime(&t);
char str[80];
sprintf(str, "%02d-%02d", tm.tm_mday, tm.tm_mon + 1);
printf("%s", str);
printf("\n");
}
Related
Which function can return current datetime with d/m/y format in C language?
EDIT:
here is my code:
#include <stdio.h>
#include <time.h>
int main()
{
time_t tmp_time;
struct tm * info;
time ( &tmp_time );
info = localtime ( &tmp_time );
printf ( "%s", asctime (info) );
}
this returns to me something like that Thu Jan 26 13:08:01 2017 and i would like to return 26/01/17 or 26/01/2017
Like this:
int main ()
{
time_t rawtime;
struct tm * currentTime;
time ( &rawtime );
currentTime = localtime ( &rawtime );
printf ( "%d/%d/%d", currentTime->tm_mday, currentTime->tm_mon+1, currentTime->tm_year+1900);
return 0;
}
Be careful, months are indexed since 0, and year is since 1900 in tm struct.
Perhaps like this:
#include <stdio.h>
#include <time.h>
int main()
{
time_t t = time(0);
if((time_t)-1 == t){
perror(0);
exit(1);
}
char buf[64];
struct tm tdata;
//I believe the 2 calls below should always succeed
//in this context
localtime_r(&t, &tdata);
strftime(buf, sizeof(buf), "%d/%m/%y", &tdata);
puts(buf);
}
The localtime(3) manpage says strftime is the recommended way to do it, and the strftime(3) manpage provides a similar example.
You can do it like this
#include <time.h>
#include <stdio.h>
int main(void)
{
time_t mytime = time(NULL);
struct tm date = *localtime(&mytime);
printf("now: %d/%d/%d\n", date.tm_mday,date.tm_mon + 1,date.tm_year +1900 );
return 0;
}
if you want to make it a function send the date as a parameter and return a int array holds day month and year
I have this code:
#include <stdio.h>
#include <time.h>
int main(void) {
time_t rawtime = time(NULL);
struct tm *ptm = localtime(&rawtime);
printf("The time is: %02d:%02d:%02d\n", ptm->tm_hour,
ptm->tm_min, ptm->tm_sec);
printf("The date is: %02d:%02d:%04d\n.", ptm->tm_mday, ptm->tm_mon, ptm->tm_year);
return 0;
}
When I run it, it returns the value of tm_year as 116 instead of 2016. Can anyone tell me why?
The tm_year field is represented as years since 1900: https://linux.die.net/man/3/localtime
As tm_year is the number of years since 1900, you need to add 1900 to this.
Source: http://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html
So you get:
#include <stdio.h>
#include <time.h>
int main(void) {
time_t rawtime = time(NULL);
struct tm *ptm = localtime(&rawtime);
printf("The time is: %02d:%02d:%02d\n", ptm->tm_hour,
ptm->tm_min, ptm->tm_sec);
printf("The date is: %02d:%02d:%04d\n.", ptm->tm_mday, ptm->tm_mon, ptm->tm_year+1900);
return 0;
}
I have to display date and time separately using gettime() and getdate() in C programming language. The code I have written only displays date and time on same line. I want this code to be done using only core C not in a windows format.The editor I am using is Visual Studio 2008
Below I have post my code which only shows date time on a single line.
#include "stdafx.h"
#include <conio.h>
#include <stdio.h>
#include <time.h>
char *gettime();
int_main(int argc,_TCHAR* argv[])
{
printf("The time is %s\n",gettime());
getch();
return 0;
}
char *gettime()
{
time_t t;
tm b;
time(&t);
return ctime(&t);
}
You can use localtime/gmtime
struct tm * timeinfo;
timeinfo = localtime (&t);
And work with tm structure components as you need, also you can use strfrtime format function if you need only print date and time.
Main function is the same for all the different ways:
#include <stdio.h>
#include <time.h>
#include <string.h>
char * gettime();
char * getdate();
int main()
{
printf("The time is %s\n", gettime());
printf("The date is %s\n", getdate());
return 0;
}
One way you could do it is with manipulating the strings coming back from ctime() function. We know they are built in a similar way, the 1st 12 chars are week-day, month, month-day, then comes 8 chars of time, then finally the year. You could create functions like this:
char * gettime()
{
time_t t;
//use static so not to save the var in stack, but in the data/bss segment
//you can also make it a global scope, use dynamic memory allocation, or
//use other methods as to prevent it from being erased when the function returns.
static char * time_str;
time(&t);
time_str = ctime(&t) + 11;
time_str[9] = 0; //null-terminator, eol
return time_str;
}
char * getdate()
{
time_t t;
static char * date_str;
static char * year;
time(&t);
date_str = ctime(&t) + 4;
date_str[6] = 0;
year = date_str + 15;
year[5] = 0;
strcat(date_str, year);
return date_str;
}
The second way to do this is using localtime() function to create a tm-struct, and then extract what you need from it.
char * gettime()
{
time_t t;
struct tm *info;
static char time_str[10];
time(&t);
info = localtime(&t);
sprintf(time_str,"%d:%d:%d",(*info).tm_hour, (*info).tm_min, (*info).tm_sec);
return time_str;
}
char * getdate()
{
time_t t;
struct tm *info;
static char date_str[12];
time(&t);
info = localtime(&t);
sprintf(date_str,"%d/%d/%d",(*info).tm_mday, (*info).tm_mon+1, (*info).tm_year+1900);
return date_str;
}
You can make it a bit more clean using the strftime() function:
char * gettime()
{
time_t t;
struct tm *info;
static char time_str[10];
time(&t);
info = localtime(&t);
strftime(time_str, 10, "%S:%M:%H",info);
return time_str;
}
char * getdate()
{
time_t t;
struct tm *info;
static char date_str[12];
time(&t);
info = localtime(&t);
strftime(date_str, 12, "%d:%m:%Y",info);
return date_str;
}
I know how to get current date & time and save it into an array. But I would like to print it in format: dd.mm.YYY_HH:MM:ss. How can I change my code to achieve this?
#include <stdio.h>
#include <time.h>
char *datetime()
{
char *array = (char*)malloc(sizeof(char)*25);
time_t result;
result = time(NULL);
sprintf(array, "%s", asctime(localtime(&result)));
array[25] = '\0';
return array;
}
int main(void)
{
// prints Sat Aug 3 18:39:07 2013
printf("%s", datetime());
// how to print:
// 03.08.2013_18:39:07
// ?
return(0);
}
Using strftime - see here
This will look like this -
time_t rawtime;
time (&rawtime);
struct tm *timeinfo = localtime (&rawtime);
strftime(array, sizeof(array)-1, "%d.%m.%y_%H:%M:%S", timeinfo);
You can use strftime function:
http://www.gnu.org/software/libc/manual/html_node/Formatting-Calendar-Time.html
strftime is described in C99 Standard in 7.23.3.5.
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(×tamp);
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);
Use struct tm Example
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.)