Difference Between Two Dates Linux BASH Shell Script Tutorial
Video Title: Difference Between Two Dates Linux BASH Shell Script Tutorial
Today we’re going to figure out how to tell how much time has elapsed between two dates if you enjoy my tutorials and would like to see more please think about contributing to my patreon account at patreon.com forward slash metalx1000 so let’s say you have two dates and you
Want to find out how many days has passed between the two of them well let’s first off look at the date command and what can be done with it so a lot can be done with it more than we’ll get today but if you just type
Date in your shell and hit enter it’s going to tell you today’s date and time right now while i’m recording this it’s saturday august 23rd it’s uh 1400 hours 3 past that 26 seconds past that and it is 2014 so it’s 203 pm to put it simply
Okay so another thing you can do to get the output of a time you can get displayed in different formats and one of those formats is going to be important for today we’re going to say is plus percent s and we hit that you can see it gives us this big
Old number what this is it’s the number of seconds that has passed since midnight on january 1st 1970 so if i run this again you can see it’s been a few seconds and if i run it every second or so it adds one second to it it’s been about four seconds since i
Ran it last yeah there we go perfect so now we know that we can convert today’s date into seconds like that can we convert other dates that have passed or ones in the future well sure we can use ctrl l to clear that i’m going to use the date command
I’m going to say plus percent s just like we did before my video finished converting okay and what we’re going to do now is say dash d and we’re going to give it a date and we’re going to give it in this format we’re going to say the year so we’ll say
2014. the month we’ll say 01 for january and i’ll say january 5th okay so if i hit enter now you can see we get the number of seconds since midnight january 1st 1970 to that date if i was to go and put in another date let’s say january 6 so we’ll say 2014
We say the january and 06 for the sixth day you can see that it is a slightly higher number so what we’re going to do here is how many seconds are in a day now there’s two ways we can figure this out we can say echo and we can say well there’s 60
Seconds in a minute we’re going to multiply that by 60 seconds in an hour sorry 60 minutes in an hour and we’re going to multiply that by 24 hours a day i’m going to pipe that into our bc our calculator here and we get 1 000 or sorry 86 400 so
We can assume right here there’s 86 400 seconds in a day that’s every day of course another way we could figure this out is we can say echo and we can say inside parentheses here that number and we can say minus in parenthesis the first date and type that into bc and
If i did this correctly um oh i’m sorry should be dollar signs here i don’t know why i missed that so what we’re saying is run this command and since we’re putting in parentheses with a dollar sign it’s going to put the output of that command so basically it’s going to put this
Number and i’m sorry that’s this number and do the same for this state and there we go we can see that yeah the dates are adding up properly when we’re using the date command because according to this from one day to the next 86 400 seconds has passed and when we did
Our math earlier it matches well so we can do the same thing for different dates so here we did from the fifth to the sixth obviously using the higher number first well how many days pass from the second to the sixth well we get again in seconds
So how do we figure this out uh in days well if we know that there’s there’s eighty six thousand four hundred seconds a day we can take this number and divide it by that and that did not work um oh because it’s dividing first right we have to put another parentheses here
And parentheses here and might have to put that in quotations there we go four days have passed with a few little mistakes there so why did we have to put up put in these things okay anytime you use the echo command you should put things in quotations
I don’t know why i wasn’t earlier but i should have been but if you do basic math you know please excuse my dear aunt sally um that you’re going to do stuff in parentheses first if not division comes before subtraction which means it was doing the division and then subtracting that number from
The other that’s why we got this number before and that’s not right so what we’re doing here and let me clear the screen just to simplify it so there’s not other stuff on the screen we’re saying okay get the number of seconds for this date and get the number of
Seconds for this date and subtract them from each other we’re doing that first because they’re inside parentheses and then after that that’s going to give us the number of seconds the number of days in seconds between the two so to get the number of days rather than the number of seconds
Because we’re smart and we know that 86 400 seconds are in a day we can divide by that number and from the second to the sixth of january on 2014 there are four days and you can put in whatever you want here let’s say uh we wanted to figure out from 2001 and
We’ll say uh we’ll say february 2nd 2001 to january 16th or sorry january 6th 2016. well we get a lot of days there and again if it’s that large then you can do more math after that to figure out the number of years and days but
We’re going to stick with days for right now because i don’t want this tutorial to go on too long i’m basically showing you the basic concept of how to do this and now it’s up to you to figure out the right math equations for the rest you’re gonna have to do
Some division based on days and take the remainders uh and get the days put that with the years you know what i’m saying so anyway from january uh sorry february 2nd 2001 to uh january 6 2016 5000 days will have passed so i hope you found this tutorial useful you can count down
Till christmas or whatever you want to count down to you can do a second counter a minute counter a day counter a year counter well if you count down to christmas that wouldn’t make sense but uh just by changing what you’re dividing by and i hope that you found this tutorial
Useful and as always i hope that you have a great day please visit my website filmsbychris.com that’s chris the k there should be a link in the description again i have a great day no i know you’re not i’m not actually obviously i speak bad more than i probably need
I would like to take a moment to thank the following patrons who made this video possible by supporting me over at patreon.com forward slash middlex1000 uh the server’s only giving you this much on downloading a file you could do four connections and speed up your download another option would be is
Sometimes servers have mirrored servers so even though you have that original server you may have another server here
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
These command are not working … Any other way is there to find the difference between two dates …
While using
date +%s – d 20201119
I get %s as Output
I need to find out differences 2timestamps any solution
This works nicely on a Linux system, but it does not work with the date command on my mac (OSX Yosemite 10.10.5):
[s000- bam@rand: /]% date +%s -d 20181225
date: illegal time format
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] …
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
ls -l `which date`
-rwxr-xr-x 1 root wheel 24400 Sep 9 2014 /bin/date
man date
DATE(1) BSD General Commands Manual DATE(1)
NAME
date — display or set date and time
SYNOPSIS
date [-ju] [-r seconds] [-v [+|-]val[ymwdHMS]] … [+output_fmt]
date [-jnu] [[[mm]dd]HH]MM[[cc]yy][.ss]
date [-jnu] -f input_fmt new_date [+output_fmt]
date [-d dst] [-t minutes_west]
The reason that I ask is that date seems to choke when converting formats if there are spaces in it.
Have used the date math like what you showed between variables that were defined in a shell script?
Thanks Kris! Very helpful
very very helpful !!! thank u so much !!
what windows manager are you useing ?? 😀
Title correction: Difference Between "two" Dates Linux BASH Shell Script Tutorial
Nice video. How about prior to 1970?
It's those trivial and short tips like these that are fun to watch.. Keep up the good work!
It's those trivial and short tips like these that are fun to watch.. Keep up the good work!