Display formatted local date and time

vegaseat 2 Tallied Votes 174 Views Share

This code gets the local time from your computer, formats the date and the time and displays the result. Does not include the bavarian time measurement of how many steins you have finished since you got up in the morning.

// display formatted local time and date (US and international)
// a Dev-C++ tested Console Application by  vegaseat  20Jan2005

#include <iostream>
#include <time.h>  // may be needed with some compilers 
 
using namespace std; 
 
int main() 
{ 
  char timebuf[32]; 
  long timeval; 
 
  time(&timeval); 
 
  // typical US display e.g.  06/15/2003 08:31:20 AM
  strftime(timebuf,32,"%m/%d/%Y %I:%M:%S %p",localtime(&timeval));
  cout << "Today's date and time is " << timebuf << endl;
    
  // different  Sun 15Jun2003 08:31:20  has 24 hour clock
  strftime(timebuf,32,"%a %d%b%Y %H:%M:%S",localtime(&timeval)); 
  cout << "Today's day, date and time is " << timebuf << endl;
 
  cin.get();  // wait
  return 0;   // optional with newer versions of C++ 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.