C program to get and set system date time in Windows

In this C program, we will learn how to get and set the systems date, time in Windows. This program will execute in Windows based compiler/system.
By IncludeHelp Last updated : March 10, 2024

In this C program, we have to set, get the system’s date and time.

To get, set the system’s date and time, we need to include ‘dos.h’ header file.

Here are the structure and function which are using in the program (all are declared in dos.h header file),

struct dosdate_t

It is a predefined structure which is used for date, time related operations, it has following members,

struct dosdate_t 
{
  unsigned char day;        /* 1--31 */
  unsigned char month;      /* 1--12 */
  unsigned int  year;       /* 1980--2099 */
  unsigned char dayofweek;  /* 0--6; 0 = Sunday */
};

_dos_getdate(&date);

It is used to get the current system date and time, assigns it to the ‘date’, which is a variable of ‘dosdate_t’ structure.

_dos_setdate(&date);

It is used to set the current system date or/and time, date or/and must be assigned in ‘date’ structure.

Program to get, set the system’s date and time in C

/*
    * program to get and set the current system date in windows
    * Compiler : turboC
*/
 
#include <stdio.h>
#include <dos.h>
 
int main()
{
    char choice;
    struct dosdate_t date; /*predefine structure to get date*/
    _dos_getdate(&date);
 
    printf("\nCurrent date is : %02d -%02d -%02d",date.day,date.month,date.year);
    printf("\nWant to change date (Y: yes):");
    choice=getchar();
 
    if(choice=='Y'||choice=='y'){
        printf("Enter new date :\n");
        printf("Enter day  :"); scanf("%d",&date.day);
        printf("Enter month:"); scanf("%d",&date.month);
        printf("Enter year :"); scanf("%d",&date.year);
 
        _dos_setdate(&date);
        printf("\nDate changed successfully.");
    }
 
    return 0;
}

Output

Current date is : 04 -07 -2012
Want to change date (Y: yes):Y
Enter new date :
Enter day  :10
Enter month:7
Enter year :2012

Date changed successfully.    

C Miscellaneous Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.