C

C program to Display Current Date and Time

In this tutorial, we are going to see how to write a C program to Display Current Date and Time. We will use the header file time.h which contains the definition of functions to manipulate Date and Time.
 

C program to Display Current Date and Time
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
  int h, min, s, day, mois, an;
  time_t now;
    
  // Returns the current time
  time(&now);

  // Convert to local time
  printf("Today is : %s", ctime(&now));

  struct tm *local = localtime(&now);
  h = local->tm_hour;        
  min = local->tm_min;       
  s = local->tm_sec;       
  day = local->tm_mday;          
  mois = local->tm_mon + 1;     
  an = local->tm_year + 1900;  

  printf("Time : %02d:%02d:%02d\n", h, min, s);

  // Display the current date
  printf("Date : %02d/%02d/%d\n", day, mois, an);

  return 0;
}

Output:

Today is : Mon Nov 15 15:41:31 2021
Time : 15:41:31
Date : 15/11/2021

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *