Write a to display present date and time using c language.

 #include <stdio.h>

#include <time.h>


int main() {

    // Get the current time

    time_t now = time(NULL);

    

    // Check if time() returns an error

    if (now == -1) {

        printf("Failed to get the current time.\n");

        return 1;

    }


    // Convert time to local time structure

    struct tm *local = localtime(&now);

    

    // Check if localtime() returns NULL

    if (local == NULL) {

        printf("Failed to convert to local time.\n");

        return 1;

    }


    // Print the current date and time in the format: YYYY-MM-DD HH:MM:SS

    printf("Current date and time: %04d-%02d-%02d %02d:%02d:%02d\n",

           local->tm_year + 1900,  // Year since 1900

           local->tm_mon + 1,      // Month (0-11)

           local->tm_mday,         // Day of the month

           local->tm_hour,         // Hours since midnight (0-23)

           local->tm_min,          // Minutes after the hour (0-59)

           local->tm_sec);         // Seconds after the minute (0-59)


    return 0;

}


Comments

Popular posts from this blog

Write a c program to Create a Circular Linked list and perform Following Operations A. Insertion At Beginning B. Insertion At End C. Insertion After a particular node Insertion Before a particular node E. Insertion at specific position F. Search a particular node G. Return a particular node H. Deletion at the beginning I. Deletion at the end J. Deletion after a particular node K. Deletion before a particular node L. Delete a particular node M. Deletion at a specific position

Write a c program to check whether the created linked list is palindrome or not

Write a c program to Create a Circular single Linked list and perform Following Operations A. Insertion After a particular node B. Insertion Before a particular node C. Search a particular node D. Return a particular node E. Deletion before a particular node F. Delete a particular node