Advertisement
metalx1000

BASH Date Command Notes

Jul 30th, 2018
959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.65 KB | None | 0 0
  1. #!/bin/bash
  2. ######################################################################
  3. #Copyright (C) 2018  Kris Occhipinti
  4. #https://filmsbykris.com
  5.  
  6. ####BASH Date command examples###
  7.  
  8. #This program is free software: you can redistribute it and/or modify
  9. #it under the terms of the GNU General Public License as published by
  10. #the Free Software Foundation, either version 3 of the License, or
  11. #(at your option) any later version.
  12.  
  13. #This program is distributed in the hope that it will be useful,
  14. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. #GNU General Public License for more details.
  17.  
  18. #You should have received a copy of the GNU General Public License
  19. #along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20. ######################################################################
  21.  
  22. wait_line(){
  23.   sleep 2
  24.   echo ""
  25. }
  26.  
  27. echo "Current Date and Time:"
  28. date
  29. wait_line
  30.  
  31. echo "The current date is:"
  32. date +%F
  33. echo "or as I write it:"
  34. date +%m/%d/%Y
  35. echo "But, you might write it:"
  36. date +%d/%m/%Y
  37. wait_line
  38.  
  39. echo "Today's Day of the Week is:"
  40. date +%a
  41. echo "or:"
  42. date +%A
  43. wait_line
  44.  
  45. echo "Current Time:"
  46. date +%H:%M:%S
  47. wait_line
  48. echo "or in 12 format and no seconds:"
  49. date +%I:%M
  50. wait_line
  51.  
  52. echo "UTC/Unix time (also known as POSIX time or UNIX Epoch time)"
  53. echo "Seconds since Thursday, 1 January 1970:"
  54. date +%s
  55. wait_line
  56.  
  57. echo "Yesterday Was:"
  58. date -d yesterday
  59. wait_line
  60.  
  61. echo "Tomorrow Will Be:"
  62. date -d tomorrow
  63. wait_line
  64.  
  65. echo "This Sunday will be:"
  66. date -d "Sunday"
  67. wait_line
  68.  
  69. echo "Last Sunday was:"
  70. date -d last-sunday +%F
  71. wait_line
  72.  
  73. echo "Next Sunday will be:"
  74. date -d "next-sunday" +%F
  75. wait_line
  76.  
  77. echo "1974-01-04 was a:"
  78. date -d "1974-01-04" +"%A"
  79. wait_line
  80.  
  81. echo "Today is the $(date +%j) day of the year"
  82. wait_line
  83.  
  84. echo "Christmas will be on a $(date -d 25-Dec +%A) this year"
  85. wait_line
  86.  
  87. TODAY=`date +%j`                 # Today, as day-of-year (1-366)
  88. CHRISTMAS=`date -d 25-Dec +%j`   # Christmas day, in same format
  89. echo "There are $(($CHRISTMAS - $TODAY)) days until Christmas."
  90. wait_line
  91.  
  92. echo "If you really want to find the number of days between two dates"
  93. echo "you should use UTC"
  94. echo "The CHRISTMAS methode won't work if the dates are in different years"
  95. wait_line
  96. echo "For example, days between 1/9/1981 and 10/22/1984:"
  97. d1="$(date -d "1/9/1981" +%s)"
  98. d2="$(date -d "10/22/1984" +%s)"
  99. echo "( $d2 - $d1 ) / (24*3600)" | bc
  100. wait_line
  101.  
  102. d1="$(date -d "1/9/1999" +%s)"
  103. d2="$(date +%s)" #today
  104. d="$(echo "( $d2 - $d1 ) / (24*3600)" | bc)"
  105. echo "It has been $d days since 1/9/1999"
  106. wait_line
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement