├── .date_calculator.sh.swp ├── README.md └── date_time_calculator.sh /.date_calculator.sh.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NisalWick2002/Linux-Terminal-Date-and-Time-Calculator/eb2f3d5badc2022ccb9de6dd6994bfda00cd4ccc/.date_calculator.sh.swp -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Linux-Terminal-Date-and-Time-Calculator 2 | The date and time calculator is a shell script for Linux. It allows adding/subtracting days, hours, and minutes from dates, calculating differences between dates, with an interactive menu-driven interface for ease of use. 3 | 4 | # Table of Contents 📚 5 | 6 | - [Overview](https://github.com/NisalWick2002/Linux-Terminal-Date-and-Time-Calculator?tab=readme-ov-file#overview-) 7 | - [Features](https://github.com/NisalWick2002/Linux-Terminal-Date-and-Time-Calculator?tab=readme-ov-file#features-%EF%B8%8F) 8 | - [Steps](https://github.com/NisalWick2002/Linux-Terminal-Date-and-Time-Calculator?tab=readme-ov-file#steps-) 9 | 10 | 11 | ## Overview 📋 12 | 13 | The date and time calculator is a versatile and efficient tool designed to perform various operations on dates and date-time strings using shell scripting in Linux. It allows users to add or subtract days, hours, and minutes from specified dates and date-time strings, as well as calculate the differences in days, hours, or minutes between two dates or date-time strings. This script leverages the built-in date command in Linux to handle these operations, making it powerful and straightforward to use. 14 | 15 | ## Features ⚙️ 16 | 17 | - Add Days to a Date: Calculate a future date by adding a specified number of days to a given date. 18 | - Subtract Days from a Date: Calculate a past date by subtracting a specified number of days from a given date. 19 | - Add Hours to a Date-Time: Calculate a future date-time by adding a specified number of hours to a given date-time. 20 | - Subtract Hours from a Date-Time: Calculate a past date-time by subtracting a specified number of hours from a given date-time. 21 | - Add Minutes to a Date-Time: Calculate a future date-time by adding a specified number of minutes to a given date-time. 22 | - Subtract Minutes from a Date-Time: Calculate a past date-time by subtracting a specified number of minutes from a given date-time. 23 | - Calculate Difference in Days Between Two Dates: Determine the number of days between two specified dates. 24 | - Calculate Difference in Hours Between Two Date-Times: Determine the number of hours between two specified date-times. 25 | - Calculate Difference in Minutes Between Two Date-Times: Determine the number of minutes between two specified date-times. 26 | 27 | ## Steps 👣 28 | 1. Save the script to a file, for example, date_time_calculator.sh 29 | 2. Open your terminal and navigate to the directory where you saved the script. Make the script executable with the following command: 30 | - chmod +x date_time_calculator.sh 31 | 3. Then run the script by typing: 32 | - ./date_time_calculator.sh 33 | -------------------------------------------------------------------------------- /date_time_calculator.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Function to add days to a date 4 | add_days() { 5 | local date=$1 6 | local days=$2 7 | local new_date=$(date -d "$date + $days days" +%Y-%m-%d) 8 | echo "Date after adding $days days: $new_date" 9 | } 10 | 11 | # Function to subtract days from a date 12 | subtract_days() { 13 | local date=$1 14 | local days=$2 15 | local new_date=$(date -d "$date - $days days" +%Y-%m-%d) 16 | echo "Date after subtracting $days days: $new_date" 17 | } 18 | 19 | # Function to add hours to a date-time 20 | add_hours() { 21 | local datetime=$1 22 | local hours=$2 23 | local new_datetime=$(date -d "$datetime + $hours hours" "+%Y-%m-%d %H:%M:%S") 24 | echo "Date-time after adding $hours hours: $new_datetime" 25 | } 26 | 27 | # Function to subtract hours from a date-time 28 | subtract_hours() { 29 | local datetime=$1 30 | local hours=$2 31 | local new_datetime=$(date -d "$datetime - $hours hours" "+%Y-%m-%d %H:%M:%S") 32 | echo "Date-time after subtracting $hours hours: $new_datetime" 33 | } 34 | 35 | # Function to add minutes to a date-time 36 | add_minutes() { 37 | local datetime=$1 38 | local minutes=$2 39 | local new_datetime=$(date -d "$datetime + $minutes minutes" "+%Y-%m-%d %H:%M:%S") 40 | echo "Date-time after adding $minutes minutes: $new_datetime" 41 | } 42 | 43 | # Function to subtract minutes from a date-time 44 | subtract_minutes() { 45 | local datetime=$1 46 | local minutes=$2 47 | local new_datetime=$(date -d "$datetime - $minutes minutes" "+%Y-%m-%d %H:%M:%S") 48 | echo "Date-time after subtracting $minutes minutes: $new_datetime" 49 | } 50 | 51 | # Function to calculate difference in days between two dates 52 | diff_days() { 53 | local date1=$1 54 | local date2=$2 55 | local diff=$(( ( $(date -d "$date2" +%s) - $(date -d "$date1" +%s) ) / 86400 )) 56 | echo "Difference between $date1 and $date2: $diff days" 57 | } 58 | 59 | # Function to calculate difference in hours between two date-times 60 | diff_hours() { 61 | local datetime1=$1 62 | local datetime2=$2 63 | local diff=$(( ( $(date -d "$datetime2" +%s) - $(date -d "$datetime1" +%s) ) / 3600 )) 64 | echo "Difference between $datetime1 and $datetime2: $diff hours" 65 | } 66 | 67 | # Function to calculate difference in minutes between two date-times 68 | diff_minutes() { 69 | local datetime1=$1 70 | local datetime2=$2 71 | local diff=$(( ( $(date -d "$datetime2" +%s) - $(date -d "$datetime1" +%s) ) / 60 )) 72 | echo "Difference between $datetime1 and $datetime2: $diff minutes" 73 | } 74 | 75 | # Menu to choose operation 76 | echo "Choose an option:" 77 | echo "1) Add days to a date" 78 | echo "2) Subtract days from a date" 79 | echo "3) Add hours to a date-time" 80 | echo "4) Subtract hours from a date-time" 81 | echo "5) Add minutes to a date-time" 82 | echo "6) Subtract minutes from a date-time" 83 | echo "7) Calculate difference in days between two dates" 84 | echo "8) Calculate difference in hours between two date-times" 85 | echo "9) Calculate difference in minutes between two date-times" 86 | read -p "Option: " option 87 | 88 | case $option in 89 | 1) 90 | read -p "Enter date (YYYY-MM-DD): " date 91 | read -p "Enter number of days to add: " days 92 | add_days $date $days 93 | ;; 94 | 2) 95 | read -p "Enter date (YYYY-MM-DD): " date 96 | read -p "Enter number of days to subtract: " days 97 | subtract_days $date $days 98 | ;; 99 | 3) 100 | read -p "Enter date-time (YYYY-MM-DD HH:MM:SS): " datetime 101 | read -p "Enter number of hours to add: " hours 102 | add_hours "$datetime" $hours 103 | ;; 104 | 4) 105 | read -p "Enter date-time (YYYY-MM-DD HH:MM:SS): " datetime 106 | read -p "Enter number of hours to subtract: " hours 107 | subtract_hours "$datetime" $hours 108 | ;; 109 | 5) 110 | read -p "Enter date-time (YYYY-MM-DD HH:MM:SS): " datetime 111 | read -p "Enter number of minutes to add: " minutes 112 | add_minutes "$datetime" $minutes 113 | ;; 114 | 6) 115 | read -p "Enter date-time (YYYY-MM-DD HH:MM:SS): " datetime 116 | read -p "Enter number of minutes to subtract: " minutes 117 | subtract_minutes "$datetime" $minutes 118 | ;; 119 | 7) 120 | read -p "Enter the first date (YYYY-MM-DD): " date1 121 | read -p "Enter the second date (YYYY-MM-DD): " date2 122 | diff_days $date1 $date2 123 | ;; 124 | 8) 125 | read -p "Enter the first date-time (YYYY-MM-DD HH:MM:SS): " datetime1 126 | read -p "Enter the second date-time (YYYY-MM-DD HH:MM:SS): " datetime2 127 | diff_hours "$datetime1" "$datetime2" 128 | ;; 129 | 9) 130 | read -p "Enter the first date-time (YYYY-MM-DD HH:MM:SS): " datetime1 131 | read -p "Enter the second date-time (YYYY-MM-DD HH:MM:SS): " datetime2 132 | diff_minutes "$datetime1" "$datetime2" 133 | ;; 134 | *) 135 | echo "Invalid option" 136 | ;; 137 | esac 138 | --------------------------------------------------------------------------------