We are going to create a interesting program today. It will be a program that displays the current time and date and also we will see that our program can add and subtract date and time based on user input values.
We will use time.h to help achieve the desired result of today's program.
Here is the program itself. The progeam today is bit long so there are two pictures in continuation.
You can see the entire code as well as copy it from below :
int main()
{
time_t currentTime;
struct tm *localTime;
time(¤tTime); // Get the current time
localTime = localtime(¤tTime); // Convert the current time to the local time
int day = localTime->tm_mday;
int month = localTime->tm_mon + 1;
int year = localTime->tm_year + 1900;
int hour = localTime->tm_hour;
int min = localTime->tm_min;
int sec = localTime->tm_sec;
printf("Current time and date: %02d-%02d-%04d %02d:%02d:%02d\n",
day, month, year, hour, min, sec);
// Add or subtract time or date
int days, hours, mins, secs;
printf("Enter the number of days, hours, minutes and seconds to add: ");
scanf("%d %d %d %d", &days, &hours, &mins, &secs);
currentTime = currentTime + (days * 24 * 60 * 60) + (hours * 60 * 60) + (mins * 60) + secs;
localTime = localtime(¤tTime);
day = localTime->tm_mday;
month = localTime->tm_mon + 1;
year = localTime->tm_year + 1900;
hour = localTime->tm_hour;
min = localTime->tm_min;
sec = localTime->tm_sec;
printf("New time and date: %02d-%02d-%04d %02d:%02d:%02d\n",
day, month, year, hour, min, sec);
return 0;
}
Let us now try to understand the program works like and then we will also see a output.
The program first includes the necessary headers, stdio.h and time.h, which provide access to the standard input/output functions and the time-related functions, respectively.
In the main function, the time_t variable currentTime is declared, which will hold the current time. The struct tm pointer localTime is also declared, which will hold the local time information. The time() function is then used to get the current time and store it in the currentTime variable.
The localtime() function is then used to convert the current time stored in currentTime to the local time, and the resulting local time information is stored in the localTime variable.
The fields tm_mday, tm_mon, tm_year, tm_hour, tm_min, and tm_sec of the tm struct are used to extract the day, month, year, hour, minute and second of the current date and time, respectively. These values are extracted by using the -> operator and added to the corresponding variables.
The printf() function is used to display the current time and date in the format "DD-MM-YYYY HH:MM:SS" using the extracted values of day, month, year, hour, minute, second.
The program then prompts the user to enter the number of days, hours, minutes, and seconds to add to the current time. These values are read using the scanf() function and stored in corresponding variables. These values are then added to the current time using the following arithmetic operation:
This arithmetic operation converts the number of days, hours, minutes, and seconds entered by the user into seconds and then add it to the current time in seconds
After the addition, the localtime() function is again used to convert the updated time in currentTime to the local time, and the resulting local time information is stored in the localTime variable. Finally, the extracted values of day, month, year, hour, minute, second are again used to display the new date and time as the result.
This program uses the standard library function time.h to get the current time and convert it to a more readable format. It also provide an interface to add or subtract time, by taking input from user in days, hours, minutes, seconds format and converts them into seconds and add to the current time and display the new time.
Output
The output of this program will be the current time and date, and then the new time and date after adding or subtracting the user-specified number of days, hours, minutes, and seconds. The output will be displayed in the format "DD-MM-YYYY HH:MM:SS"
It indicates the current date and time as 11-01-2023 21:34:56 and after adding 1 day, 2 hours, 3 mins and 4 secs the new date and time will be 12-01-2023 23:38:00
Note that the actual output will depend on the current time and the values entered by the user when prompted.
If you also want to learn coding then find your teacher according to your language. For me i am comfortable with hindi language and follow hindi teachers.
You can refer to my teacher below in case you are also comfortable with hindi.
I will soon call myself a trader and a small programmer together.
Happy trading and keep learning what you love.
@tipu curate
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Upvoted 👌 (Mana: 1/4) Get profit votes with @tipU :)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit