1

I want to convert a date into a timestamp.

My input looks something like:

Mon Sep 30 17:37:39 2019 

And I want to take that timestamp and check how many days that have passed since today.

What I tried:

data = "$(date --date "$(echo "$info" | grep "Date added: *" | sed 's/Date added\:\s//i' | awk '{$1=$1};1')" '+%H')"
final = $(expr $(date '+%H') - $data)  

However this will in some cases print out values such as -9, -3, -12 etc and for todays date it currently print out "08". Any ideas how to solve this?

Something that is worth mentioning is that I dont really care about exact hours if that complicates things, I am more interesting in how many days that have passed.

EDIT

Current solution (does not handle hours which is a bummer). I extract the Day, Month and Year and organise it into YYYY-MM-DD and thereafter convert it to a timestamp. This solution do not handle hours which I do want in the end.

$(( ($(expr $(date '+%s')) - $(date -d "$(printf '%s\n' "$date_added" | awk '{
        printf "%04d-%02d-%02d\n", $5, \
        (index("JanFebMarAprMayJunJulAugSepOctNovDec",$2)+2)/3,$3}')" +"%s")) / 3600 )) 

1 Answer 1

2

EDIT: Adding a generic solution(after doing some testing and searches(google)), could you please try following which will provide exact days, hours, minutes and seconds difference between 2 dates.

prev=$(date --date='Mon Aug 30 14:46:39 2019' +"%s")
today=$(date +%s)
number_of_seconds=$(( ($today - $prev ) ))
printf '%dd:%dh:%dm:%ds\n' $(($number_of_seconds/86400)) $(($number_of_seconds%86400/3600)) $(($number_of_seconds%3600/60)) $(($number_of_seconds%60))

Output is as follows(I am in on my aws server with UTC time zone in it).

56d:17h:29m:14s
##### Where my system's date is: #####
date
Sat Oct 26 08:16:14 UTC 2019


Could you please try following once.

prev=$(date --date='Mon Sep 30 17:37:39 2019' +"%s")
today=$(date +%s)
echo $(( ($today - $prev )/(60*60*24) ))

Output I am getting it 25 since I am in IST time zone and it is around 12:20 PM IST as of now. Also variable names are self explanatory, where prev variable is for date which OP has asked to get the difference from today's date.

Explanation of getting days difference:

  1. First I am passing OP's given date to date --date... command to get it in epoch time and saving it into prev bash variable.
  2. Now creating today bash variable which has today's epoch time.
  3. Then taking difference of these 2 variables epoch time values and converting them into exact Number of days by dividing 60*60*24 to its difference epoch value.


NOTE: Tested on different dates for testing like for 30th Aug 2019:

prev=$(date --date='Mon Aug 30 17:37:39 2019' +"%s")
today=$(date +%s)
echo $(( ($today - $prev )/(60*60*24) ))
56
Sign up to request clarification or add additional context in comments.

6 Comments

I like the idea, but it doesnt seem to work, I you try other dates you will see that the nuimber 25 will occur again. Thinking that the format on the date is wrong. I found a temporary solution but it does not handle hours (just days), will update the post with what I found.
@Fredrik, Hey I just checked for another day and number of days are coming perfectly. Only thing which I need to edit/add is for hours calculation, which I will add in sometime and let you know then.
Alright, tried a few things and it works very well! Any ideas how I can include hours?
@Fredrik, yeah I am looking into it buddy, will let you know in few mins:)
Did some further testing at I think the code you made actually do handle hours/minutes/seconds too. Not 100% sure but one time is: Fri Oct 25 22:24:20 amd compare it to now that is (1572077267 - 1572042260) /3600 => 9.69, It is two hours late but I guess that is due to time difference from the site I used. But since it is not 0 means it do handle hours within a day :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.