// Written by Scott Johnson - SoftwareNuggets
// Youtube :: https://[Log in to view URL]

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

int main() {
    time_t now;
    struct tm *timeinfo;
    char buffer[80];
    
    printf("Current Date and Time Demo\n");
    printf("==========================\n\n");
    
    /* Get current time */
    time(&now);
    
    /* Method 1: Using ctime() - simple string format */
    printf("Method 1 - ctime():\n");
    printf("Current time: %s\n", ctime(&now));
    
    /* Method 2: Using localtime() and manual formatting */
    timeinfo = localtime(&now);
    
    printf("Method 2 - Manual formatting:\n");
    printf("Date: %02d/%02d/%04d\n", 
            timeinfo->tm_mon + 1,      /* months are 0-11  */
           timeinfo->tm_mday,          /* actual day       */
           timeinfo->tm_year + 1900);  /* years since 1900 */
    
    printf("Time: %02d:%02d:%02d\n", 
           timeinfo->tm_hour, 
           timeinfo->tm_min, 
           timeinfo->tm_sec);

    /*
     *
     Date specifiers:

        %A - Full weekday name (Monday, Tuesday, etc.)
        %a - Abbreviated weekday name (Mon, Tue, etc.)
        %B - Full month name (January, February, etc.)
        %b - Abbreviated month name (Jan, Feb, etc.)
        %Y - 4-digit year (2025)
        %y - 2-digit year (25)
        %m - Month as number with leading zero (01-12)
        %d - Day of month with leading zero (01-31)
        %e - Day of month without leading zero (1-31)

     Time specifiers:

        %H - Hour in 24-hour format with leading zero (00-23)
        %I - Hour in 12-hour format with leading zero (01-12)
        %M - Minutes with leading zero (00-59)
        %S - Seconds with leading zero (00-59)
        %p - AM/PM indicator
        %T - Time in HH:MM:SS format (equivalent to %H:%M:%S)

     Other useful ones:

        %c - Complete date and time representation
        %x - Date representation for current locale
        %X - Time representation for current locale
        %j - Day of year (001-366)
        %U - Week number of year (00-53, Sunday as first day)
        %W - Week number of year (00-53, Monday as first day)
    *
    */


    
    /* Method 3: Using strftime() for custom formatting */
    printf("\nMethod 3 - strftime() custom formats:\n");
    
    strftime(buffer, sizeof(buffer), "%A, %B %d, %Y", timeinfo);
    printf("Full date: %s\n", buffer);
    
    strftime(buffer, sizeof(buffer), "%I:%M:%S %p", timeinfo);
    printf("12-hour time: %s\n", buffer);
    
    strftime(buffer, sizeof(buffer), "%H:%M:%S", timeinfo);
    printf("24-hour time: %s\n", buffer);
    
    strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeinfo);
    printf("ISO format: %s\n", buffer);
    
    /* Display individual components */
    printf("\nDetailed breakdown:\n");
    printf("Year: %d\n", timeinfo->tm_year + 1900);
    printf("Month: %d (%s)\n", timeinfo->tm_mon + 1, 
           (timeinfo->tm_mon == 0) ? "January" :
           (timeinfo->tm_mon == 1) ? "February" :
           (timeinfo->tm_mon == 2) ? "March" :
           (timeinfo->tm_mon == 3) ? "April" :
           (timeinfo->tm_mon == 4) ? "May" :
           (timeinfo->tm_mon == 5) ? "June" :
           (timeinfo->tm_mon == 6) ? "July" :
           (timeinfo->tm_mon == 7) ? "August" :
           (timeinfo->tm_mon == 8) ? "September" :
           (timeinfo->tm_mon == 9) ? "October" :
           (timeinfo->tm_mon == 10) ? "November" : "December");
    printf("Day: %d\n", timeinfo->tm_mday);
    printf("Hour: %d\n", timeinfo->tm_hour);
    printf("Minute: %d\n", timeinfo->tm_min);
    printf("Second: %d\n", timeinfo->tm_sec);
    printf("Day of week: %d (hint 0=Sunday)\n", timeinfo->tm_wday);
    printf("Day of year: %d\n", timeinfo->tm_yday + 1);

    printf("Daylight saving time: %s\n", 
           timeinfo->tm_isdst > 0 ? "Yes" : 
           timeinfo->tm_isdst == 0 ? "No" : "Unknown");
    
    /* Display UTC time for comparison */
    printf("\nUTC Time (for comparison):\n");
    struct tm *utc_time = gmtime(&now);
    strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S UTC %z", utc_time);
    printf("UTC: %s\n", buffer);
    
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: