473,408 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How to get the current date information only?

I am a newbie of C and I need to do a program to get the current date
information only, without the time. I have my code here:

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

int main ()
{
time_t rawtime;
struct tm * timeinfo;
char* t;

rawtime = time (NULL);
timeinfo = localtime (&rawtime);
t = asctime(timeinfo);
printf(t);

return 0;
}

How to output only the year, month and day of t?

Jul 6 '06 #1
14 8571
YiMkiE wrote:
I am a newbie of C and I need to do a program to get the current date
information only, without the time. I have my code here:

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

int main ()
{
time_t rawtime;
struct tm * timeinfo;
char* t;

rawtime = time (NULL);
timeinfo = localtime (&rawtime);
t = asctime(timeinfo);
printf(t);

return 0;
}

How to output only the year, month and day of t?
Use the appropriate fields in 'timeinfo'.

--
Ian Collins.
Jul 6 '06 #2
YiMkiE wrote:
I am a newbie of C and I need to do a program to get the current date
information only, without the time. I have my code here:

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

int main ()
{
time_t rawtime;
struct tm * timeinfo;
char* t;

rawtime = time (NULL);
timeinfo = localtime (&rawtime);
t = asctime(timeinfo);
printf(t);

return 0;
}

How to output only the year, month and day of t?
Lookup the strftime function.

Robert Gamble

Jul 6 '06 #3
Do you mean the tm_year? How to refer to that?
I know this is a stupid question..

Ian Collins 寫道:
YiMkiE wrote:
I am a newbie of C and I need to do a program to get the current date
information only, without the time. I have my code here:

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

int main ()
{
time_t rawtime;
struct tm * timeinfo;
char* t;

rawtime = time (NULL);
timeinfo = localtime (&rawtime);
t = asctime(timeinfo);
printf(t);

return 0;
}

How to output only the year, month and day of t?
Use the appropriate fields in 'timeinfo'.

--
Ian Collins.
Jul 6 '06 #4
"YiMkiE" <yi****@gmail.comwrites:
I am a newbie of C and I need to do a program to get the current date
information only, without the time. I have my code here:

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

int main ()
{
time_t rawtime;
struct tm * timeinfo;
char* t;

rawtime = time (NULL);
timeinfo = localtime (&rawtime);
t = asctime(timeinfo);
printf(t);

return 0;
}

How to output only the year, month and day of t?
You either can lookup the members of the struct tm structure
and display only the fields you want or you can use the strftime
function that generates formatted text using different date/time
format specifiers.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jul 6 '06 #5
YiMkiE (in 11**********************@a14g2000cwb.googlegroups. com)
said:

| I am a newbie of C and I need to do a program to get the current
| date information only, without the time. I have my code here:
|
| #include <stdio.h>
| #include <time.h>
| #include <string.h>
|
| int main ()
| {
| time_t rawtime;
| struct tm * timeinfo;
| char* t;
|
| rawtime = time (NULL);
| timeinfo = localtime (&rawtime);
| t = asctime(timeinfo);
| printf(t);
|
| return 0;
| }
|
| How to output only the year, month and day of t?

Just write your own function to build the string you want:

char *YiMkiE_date(const struct tm *t)
{ char *mon_name[12] =
{ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static char date[] = "YYYY MMM DD\n";

sprintf(date,"%d %s %d\n", 1900 + t->tm_year,
mon_name[t->tm_mon], t->tm_mday);
return date;
}

Easy, isn't it?

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Jul 6 '06 #6
Thanks all, I have solved the date problem. But now I am getting
another trouble.
I need to concatenate a string and the date string I got to form a file
name.

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

int main()
{
int e;
FILE *fp;
char fname[13] = "AMH_TdySales-";
time_t rawtime;
struct tm * timeinfo;
char t[9];

rawtime = time (NULL);
timeinfo = localtime (&rawtime);
strftime(t ,10 , "%Y%m%d", timeinfo);

strncat(fname, t, 8);
printf(fname);

fp = fopen(fname, "r");
if (fp == NULL)
e = 0;
fclose(fp);

fp = fopen("log.txt", "a");
if (e == 0)
fprintf(fp, "%s - File not found!!\n", fname);
fclose(fp);

return 0;
}

After concatenating the two, the end of line (or array?) character
appears in the middle, causing error. How to remove that?

Jul 6 '06 #7
jjf

Morris Dovey wrote:
YiMkiE (in 11**********************@a14g2000cwb.googlegroups. com)
said:

| I am a newbie of C and I need to do a program to get the current
| date information only, without the time. I have my code here:
|
| #include <stdio.h>
| #include <time.h>
| #include <string.h>
|
| int main ()
| {
| time_t rawtime;
| struct tm * timeinfo;
| char* t;
|
| rawtime = time (NULL);
| timeinfo = localtime (&rawtime);
| t = asctime(timeinfo);
| printf(t);
|
| return 0;
| }
|
| How to output only the year, month and day of t?

Just write your own function to build the string you want:

char *YiMkiE_date(const struct tm *t)
{ char *mon_name[12] =
{ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static char date[] = "YYYY MMM DD\n";

sprintf(date,"%d %s %d\n", 1900 + t->tm_year,
mon_name[t->tm_mon], t->tm_mday);
return date;
}

Easy, isn't it?
Not compared with using strftime(), which also looks after locales for
you. Why re-invent the wheel?

Jul 6 '06 #8
In article <11**********************@p79g2000cwp.googlegroups .com>
YiMkiE <yi****@gmail.comwrote:
>#include <stdio.h>
#include <time.h>
#include <string.h>

int main()
{
int e;
FILE *fp;
char fname[13] = "AMH_TdySales-";
This array has room for 13 "char"s. The initializer:

"AMH_TdySales-\0"
0 1
1234567890123***ran off the end here

*does* fit, just barely, by C's rule that says that the terminating
'\0' of a string literal initializer is discarded when initializing
an array whose size is just big enough to hold everything except the
'\0'.

Or, to put it another way, had you written:

char fname[] = "AMH_TdySales-";

the array would have size *14*, not 13.

The end result is that fname[] contains a sequence of bytes that
is *not* terminated with a '\0', and is therefore not a string
(by definition).
time_t rawtime;
struct tm * timeinfo;
char t[9];
The array "t" has size 9 (in "C bytes", aka chars).
rawtime = time (NULL);
timeinfo = localtime (&rawtime);
strftime(t ,10 , "%Y%m%d", timeinfo);
The array "t" has size 9. Why did you tell strftime() to write at
most *ten* characters into a 9-character array? However, %Y needs
4, %m needs 2, and %d needs 2; and 4+2+2 = 8 -- so strftime() will
write the appropriate 8 characters, then add a ninth '\0' character
to make the result a string, and this does fit.
strncat(fname, t, 8);
The strncat() function needs its first argument to be a string --
a sequence of "char"s terminated by a '\0' character. fname does
not hold a string, so the effect is undefined.

Even if it were OK, this tells strncat to add at most 8 characters
to the original string. If the original string has 13 non-'\0'
characters followed by a '\0', the resulting string will have 13+8
= 21 non-'\0' characters followed by a '\0'. So it needs at least
22 bytes of space.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jul 6 '06 #9
Solved!
Thanks very much Chris!

Chris Torek 寫道:
In article <11**********************@p79g2000cwp.googlegroups .com>
YiMkiE <yi****@gmail.comwrote:
#include <stdio.h>
#include <time.h>
#include <string.h>

int main()
{
int e;
FILE *fp;
char fname[13] = "AMH_TdySales-";

This array has room for 13 "char"s. The initializer:

"AMH_TdySales-\0"
0 1
1234567890123***ran off the end here

*does* fit, just barely, by C's rule that says that the terminating
'\0' of a string literal initializer is discarded when initializing
an array whose size is just big enough to hold everything except the
'\0'.

Or, to put it another way, had you written:

char fname[] = "AMH_TdySales-";

the array would have size *14*, not 13.

The end result is that fname[] contains a sequence of bytes that
is *not* terminated with a '\0', and is therefore not a string
(by definition).
time_t rawtime;
struct tm * timeinfo;
char t[9];

The array "t" has size 9 (in "C bytes", aka chars).
rawtime = time (NULL);
timeinfo = localtime (&rawtime);
strftime(t ,10 , "%Y%m%d", timeinfo);

The array "t" has size 9. Why did you tell strftime() to write at
most *ten* characters into a 9-character array? However, %Y needs
4, %m needs 2, and %d needs 2; and 4+2+2 = 8 -- so strftime() will
write the appropriate 8 characters, then add a ninth '\0' character
to make the result a string, and this does fit.
strncat(fname, t, 8);

The strncat() function needs its first argument to be a string --
a sequence of "char"s terminated by a '\0' character. fname does
not hold a string, so the effect is undefined.

Even if it were OK, this tells strncat to add at most 8 characters
to the original string. If the original string has 13 non-'\0'
characters followed by a '\0', the resulting string will have 13+8
= 21 non-'\0' characters followed by a '\0'. So it needs at least
22 bytes of space.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jul 6 '06 #10
"YiMkiE" <yi****@gmail.comwrites:
Solved!
Thanks very much Chris!
Please read <http://www.caliburn.nl/topposting.html>. Thanks.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 6 '06 #11

Keith Thompson 寫道:
"YiMkiE" <yi****@gmail.comwrites:
Solved!
Thanks very much Chris!

Please read <http://www.caliburn.nl/topposting.html>. Thanks.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
ooops, sorry!
will pay attention to this!

Jul 6 '06 #12
YiMkiE wrote:
>
Keith Thompson 寫道:
"YiMkiE" <yi****@gmail.comwrites:
Solved!
Thanks very much Chris!
Please read <http://www.caliburn.nl/topposting.html>. Thanks.

--
Keith Thompson (The_Other_Keith) ks***@mib.org
<http://www.ghoti.net/~kstSan Diego Supercomputer Center
<* <http://users.sdsc.edu/~kstWe must do something. This is
something. Therefore, we must do this.

ooops, sorry!
will pay attention to this!
Also trim the quoted material. In particular, remove .sigs (the bits
after the --). A good newsreader does that for you, alas you are using
Google so you have to do it manually.

Brian
Jul 6 '06 #13
I am using google. How to read this group in outlook express or some
other newsreader? (though this may be a bit off topic)

Jul 7 '06 #14
"YiMkiE" <yi****@gmail.comwrote in message
news:11**********************@k73g2000cwa.googlegr oups.com...
>I am using google. How to read this group in outlook express or some
other newsreader? (though this may be a bit off topic)
<totally OT response>
Click on one of these:
http://www.elfqrin.com/hacklab/pages/nntpserv.php
</totally OT response>
Jul 7 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Norvell | last post by:
I just noticed that recently the javascript I was using to format and display the last modified date of my web page is always reporting the current date instead. It used to work fine. This is true...
13
by: Luigi | last post by:
Imagine I have a small, static, personal site (I do, actually: http://kirpi.it/). And imagine that, just below the title of the page, I would like to have a comment, or a news line, or a birthday...
1
by: Keith Crooks | last post by:
I have an access database with a field set to the current date (this is taken from the system date how can i extract from that field just the month and year. or have a field that gives me only...
5
by: Larry R Harrison Jr | last post by:
I use the Lebans calendar control and love it; the only thing is that it commonly starts out at the year 1899 rather than the current year. Is there a way to specifically tell it to start at the...
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
6
by: vijayk | last post by:
Hi all, I have a field which has data as YYYYMMDD, and I have to find the age of the person by substracting it from current date. can you please please advice... thanks
1
by: daddio | last post by:
I'm a divorced dad that travels constantly with my job and I wanted a way to let my daughter know where I am. I built a simple little website but it needs a little bit of complexity in order to...
4
mbolan77
by: mbolan77 | last post by:
Hello... I have a table with a number of fields along with a time stamp column... I am wondering how I can query for only rows inserted on the current date other than manually entering the date...
5
by: bruce24444 | last post by:
I have a database which assigns warranty claims to people with a main screen showing number of files assigned to each person. The number assigned shows day, week, month and year numbers so they can...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.