system dates

Posted on 2000-04-20
Last Modified: 2010-04-15
I am writting a C program and I need to display the current system date.  What is the syntax, what header file do I need etc...
Question by:abeveridge

Expert Comment

ID: 2736742
One way...

#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>

void main()
  time_t ltime;

  time( &ltime );
  printf( "Time in seconds since UTC  1/1/70:\t%ld\n", ltime );
  printf( "time and date:\t\t\t%s", ctime( &ltime ) );


Accepted Solution

kishore_joshi earned 75 total points
ID: 2737061
check out this simple example to get the system date and time.....

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

int main(void)
  time_t t;

  t = time(NULL);

  printf("Current date is %s", ctime(&t));

  t -= 24L*60L*60L;  /* Back up to same time previous day */

  printf("\nNew date is %s", ctime(&t));

  return 0;
LVL 84

Expert Comment

ID: 2737073
/* Back up to same time previous day */
Assuming no daylight savings switch

Expert Comment

ID: 2825050
As Ozo and Kishori Joshi put it, the answer of your question can be put in this way. And you can even see the output of this program from the output file.

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

long int        time();
char           *ctime();
  FILE *fp, *fopen();
int main(void)

  time_t clock;

  clock = time(NULL);
        fp = fopen("goandsee", "w");
        if((fp = fopen("goandsee", "w")) == NULL){
        printf("Error for writing output file.\n");

  fprintf(fp, "Current date is %s", ctime(&clock));

  clock -= 24L*60L*60L;  /* Back up to same time previous day */

  fprintf(fp, "\n New date is %s", ctime(&clock));

  return 0;


Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now