You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To restrict users from selecting previous dates and only allow selection of the current date or future dates in C programming, you can modify your code as follows. Here, I'll provide a console-based example that checks for valid date selection:
#include <stdio.h>
#include <time.h>
#define NUM_DATES 31 // Assuming 31 date cells
int main() {
int selectedDates[NUM_DATES] = {0}; // Initialize an array to track selected dates
int currentDate = 0; // Initialize to no date selected
int selectedDate;
// Get the current date
time_t t;
struct tm *tm_info;
time(&t);
tm_info = localtime(&t);
currentDate = tm_info->tm_mday; // Current day of the month
printf("Today's date is the %dth\n", currentDate);
// Simulate user selecting a date (for example, the 10th)
selectedDate = 10;
// Check if the selected date is valid
if (selectedDate >= currentDate) {
selectedDates[selectedDate] = 1; // Mark the selected date in the array
} else {
printf("Invalid selection. You can only select today's date or a future date.\n");
}
// Display date cells and change colors based on selection
for (int i = 1; i <= NUM_DATES; i++) {
if (selectedDates[i] == 1) {
printf("\x1b[31m"); // Change text color to red (you can customize this)
}
printf("%2d ", i);
if (selectedDates[i] == 1) {
printf("\x1b[0m"); // Reset text color to default
}
}
return 0;
Hi,
I need to restrict previous dates to current date.
How can i make to this?
Sorry my poor English.
The text was updated successfully, but these errors were encountered: