Help: Display time and date

This is a discussion on Help: Display time and date within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, can someone tell me how to write a program to display the current time and date, and copy ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    1

    Help: Display time and date

    Hi everyone,

    can someone tell me how to write a program to display the current time and date, and copy to a text file whenever the program executed?

    Thanks

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Look up some information on file I/O and C++'s built in time and date functions.
    Code:
    <time.h>
    <fstream.h>
    Read some tutorials on those. Read the libraries themselves.

    Oh, and have fun
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    5

    try this

    create a new win32 console application

    insert this code in it.

    Code:
         // we need the time and date
    	 CTime t = CTime::GetCurrentTime();
    
    	// date
    	cout << "Date: " << t.GetYear() << "-";
    	if (t.GetMonth()<10) 
    		cout << "0";
    	cout << t.GetMonth() << "-";
    	if (t.GetDay()<10) 
    		cout << "0";
    	cout << t.GetDay() << "\r\n";
    	// time
    	cout << "Time: ";
    	if (t.GetHour()<10)
    			cout << "0";
    	cout <<  t.GetHour() << ":";
    	if (t.GetMinute()<10)
    			cout << "0";
    	cout <<  t.GetMinute() << ":";
    	if (t.GetSecond()<10)
    			cout << "0";
    	cout <<  t.GetSecond() << "\r\n";
    
    	getch();

    you will need to include the proper library if the compiler ask for it.

    BR

    froque

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    using CTime

    Isn't CTime part of MFC. If it is you will also need to go to project -settings and select USING MFC IN STATIC LIBRARY. You will also have to include afxwin.h
    zMan

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Since when did the question concern MFC?
    can someone tell me how to write a program to display the current time and date, and copy to a text file whenever the program executed?
    Not a single mention of MFC or even Windows. In the absence of such mention, let's try and stick with something portable, kay?
    Code:
    #include <iostream>
    #include <fstream>
    #include <ctime>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        if (argc == 2) {
            ofstream log(argv[1]);
    
            if (!log)
                cerr<<"Logging disabled\n";
            else {
                time_t present = time(0);
                log<< ctime(&present) <<endl;
                log.close();
            }
        }
        else if (argc > 1)
            cerr<<"usage: $"<< *argv <<" <logfile>\n";
    
        // Rest of the program
    }
    My best code is written with the delete key.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    And, there's more date & time info in the Programming FAQ!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  3. Todays time and date
    By wozza in forum C Programming
    Replies: 7
    Last Post: 05-27-2002, 03:42 PM
  4. FAQ: How do i display current time and date ?
    By davie_scotland in forum FAQ Board
    Replies: 2
    Last Post: 01-24-2002, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21