How may I pipe a timestamp to date and convert it to datetime? [duplicate] - bash

This question already has answers here:
Pipe string to GNU Date for conversion - how to make it read from stdin?
(5 answers)
Closed 4 years ago.
The community reviewed whether to reopen this question 11 months ago and left it closed:
Original close reason(s) were not resolved
Assume we have a timestamp like below:
[xxxxxx ~]$ date -d#1530586185
Tue Jul 3 05:49:45 EEST 2018
and I want to pipe it to date like:
echo 1530586185 | date -d#
date: invalid date ‘#’ <-------- error message
How can I do it?

Suggestion 1:
date -d"#$(echo 1530586185)"
Suggestion 2:
DATE=$(echo 1530586185); date -d"#$DATE"

Related

Shell: How to calculate current MMYY to previous date (-15 months)

I am trying to calculate the current date MMYY - 15 months ago.
I get current date in the format I want with shell command: date +"%m%y"
I am trying to use this current date and calculate what the MMYY is 15 months ago, what command would show me this?
Not sure why you want to get the current MMYY and calculate "15 months ago" afterwards. You could just do it in one command:
date --date='15 months ago' +"%m%y"
man date and info date are your friend.

Get the days between two dates which are in Unix Timestamp [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Have a file with pull-requests details and has CreatedDate (ex: 1613698170) is in Unix timestamp format.
I want to notify to stake holders, when pull-request is open more than x days.
How to get the no of days between current date and pull-request created date in bash / groovy. So that I will execute this script in jenkins and send out notifications.
The following code:
def epochMillis = 1598098239000
def now = new Date()
def then = new Date(epochMillis)
def days = now - then
println "then: $then"
println "now: $now"
println "days between now and then: ${days}"
calculates the number of days between now and epoch millis. When executed, the above prints:
─➤ groovy solution.groovy
then: Sat Aug 22 14:10:39 CEST 2020
now: Fri Feb 19 19:01:54 CET 2021
days between now and file creation: 181

Best way to compare Timestamps in Linux shell/bash script? [duplicate]

This question already has answers here:
Bash script compare two date variables [duplicate]
(5 answers)
Closed 1 year ago.
I have to read a epoch timestamp (in seconds) from a directory /usr/local/healthcheck.txt on my Red Hate Enterprise Linux machine every ~10 minutes (polling). I need perform a comparison on the time to check if the timestamp in the healthcheck.txt file is OLDER than 50 minutes from the current time/timestamp OR if the healthcheck.txt is non-existent, to throw an error. The timestamp in the healthcheck.txt file generally looks like this (its in seconds, as stated above) :
1591783065
I was using date -d #1591783065 to convert the timestamp to Human Readable and get something like this:
Tue Jun 9 16:22:57 UTC 2020
What would be the best approach to compare the current timestamp to this timestamp in the file and check if its older than 50 minutes?
In Java , we have a Date package , and can just use compareTo to compare the times/dates, is there a simple way to do this with shell/bash scripts?
Why don't you stick with epoch-time? You can get the current time as seconds since epoch by
date +%s, so you just have to compare
if (( (healthcheck_time + 50*60) < $(date +%s) ))
then
# .... healthcheck older than 50 minutes
fi

Calculate time passed since $STARDATE and current date [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I want to establish the amount of time that has passed since 24th February 2008 - time 17:28 UTC
The output would be something like:
Its been x years x months x days x hours x minute x seconds since Tottenham Won a Trophy
You can use python:
python -c "from datetime import datetime as d; print(d.today() - d(2008, 2, 24, 17, 28, 0))"
Or you have to build something like:
starttime=$(date -d "2008-02-24 17:28 UTC" +%s)
current=$(date +%s)
timediff=$((current - starttime))
echo $timediff
days=$((timediff/86400))
hours=$((timediff%86400/3600))
min=$((timediff%3600/60))
sec=$((timediff%60))
echo "$days days $hours hours $min minutes $sec seconds since start"
Years and month would be more difficult

Strange behavior for the date() function linux [duplicate]

This question already has an answer here:
bash "date" returns "invalid date" error for a specific date string
(1 answer)
Closed 7 years ago.
I have found a strange behaviour for the date() function when using bash.
When I use date -d "2008-10-12 +1 days", the date function returned
date: invalid date ‘2008-10-12 +1days’
Even it returned error when trying:
date -d "2008-10-12"
--> date: invalid date ‘2008-10-12’
However, it works for:
date -d "2008-10-13 +1days"
--> Tue Oct 14 00:00:00 CLST 2008
date -d "2008-10-11 +1days"
--> Sun Oct 12 01:00:00 CLST 2008
date -d "2007-10-12 +1days"
--> Sat Oct 13 00:00:00 CLT 2007
... and so on
It seems the date() does not recognize "just" 2008-10-12. Is it a bug? Am I doing something wrong?
I am using CENTOS 7
All the best
In RHEL 7 there were many changes happened to somany commands. To change the time and date earlier we used to use date command and now we need to use timedatectl command (preferred) instead.
http://www.freedesktop.org/software/systemd/man/timedatectl.html
So as there is a new latest command with more features included they might have limited it (Not sure on that). But from this version I have been using timedatectl command which is really great one.

Resources