Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 28th, 2005, 10:11 PM   #1
Navid
Hobbyist Programmer
 
Navid's Avatar
 
Join Date: Feb 2005
Location: Canada
Posts: 187
Rep Power: 4 Navid is on a distinguished road
Send a message via MSN to Navid
Getting the Date

How can I get the current date with C? Like the year,month,day,hour,minute and second.
Navid is offline   Reply With Quote
Old Jun 28th, 2005, 10:20 PM   #2
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
Quote:
Originally Posted by navnav
How can I get the current date with C? Like the year,month,day,hour,minute and second.
maybe smth like this?
system("date");
on gcc.
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics
EverLearning is offline   Reply With Quote
Old Jun 28th, 2005, 10:23 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Refer to your documentation regarding "time", "asctime", "mktime". You'll encounter additional references along the way. Essentially, you'll start with the number of seconds elapsed since the epoch (January 1, 1970) and feed that to other functions to fill in a structure containing the things you want. The precise functions may turn out to be wrappers for newer functions using 64-bit rather than 32-bit values. Mileage varies, but it's all there for the picking.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 28th, 2005, 10:30 PM   #4
Navid
Hobbyist Programmer
 
Navid's Avatar
 
Join Date: Feb 2005
Location: Canada
Posts: 187
Rep Power: 4 Navid is on a distinguished road
Send a message via MSN to Navid
But i need to get those values without prompting, like call a function and return the year value(2005) to a variable.

Like in JavaScript I would write:
<script language="javascript">
year = new Date().getFullYear();
</script>
Navid is offline   Reply With Quote
Old Jun 28th, 2005, 10:42 PM   #5
Navid
Hobbyist Programmer
 
Navid's Avatar
 
Join Date: Feb 2005
Location: Canada
Posts: 187
Rep Power: 4 Navid is on a distinguished road
Send a message via MSN to Navid
hmm...
I'll just request it to be inputed from the user for now. But thanks i didnt know about those dos commands.
Navid is offline   Reply With Quote
Old Jun 28th, 2005, 10:46 PM   #6
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
Here's something else I found:
clicky
Then scroll down to the time.h and look at the example program, seems like it has what you you are looking for, which is basically same as what DaWei said.
Only this is a ready-made code

ps: I couldn't read the descriptions of the time functions, Mozilla times me out but example compiled and worked.
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics
EverLearning is offline   Reply With Quote
Old Jun 28th, 2005, 11:30 PM   #7
nindoja
Programmer
 
Join Date: Jun 2005
Posts: 92
Rep Power: 4 nindoja is on a distinguished road
This works on windows systems:

char dateStr[9];
_strdate(dateStr);
nindoja is offline   Reply With Quote
Old Jun 29th, 2005, 7:59 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
But i need to get those values without prompting, like call a function and return the year value(2005) to a variable.
So what do you suppose those functions I referred you to, do in various ways? Churn butter? You need to evaluate your responses and follow up on them. It's part of learning to do the job. Sometimes your respondents will actually write the code for you, sometimes not.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 29th, 2005, 8:25 AM   #9
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Like post #3 said, fill the tmtime structure and get your information from there.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Jun 29th, 2005, 8:41 AM   #10
Nuticulus
Programmer
 
Nuticulus's Avatar
 
Join Date: May 2005
Location: England
Posts: 61
Rep Power: 4 Nuticulus is on a distinguished road
Like posts above, I recommend you use time.h. It's full of functions for getting the time. Here's a reference. Nice and simple.

If you need microseconds, you can use gettimeofday() under *nix. Read the manual pages for how to use it. I can't remember how to get the time in milliseconds under Windows right now. It may come back to me later.
Nuticulus is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:59 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC