Use variable within unix date command - linux

OS: CentOs 6 Final 64 Bit
I'm trying to write a shell script which gets the date a given number of days ago. The number of days is dynamic and the code I'm trying to use is this...
date +%d-%m-%Y -d '$days days ago'
The response I get back is...
date: invalid date `$days days ago'
Can anyone suggest to me the correct format to code this as I'm pulling my hair out and soon my teeth!!
Cheers
Paul

Change single quote ' to double quote ". Otherwise the string $days will be sent instead of its value.

Try:
[matias#lappie ~]$ date +%d-%m-%Y -d "now - $days days"
04-02-2015

Related

How to convert a bash string to date format? [duplicate]

This question already has answers here:
How to convert YYYYMMDDHHMMSS to a date readable by `date`
(6 answers)
Closed 10 months ago.
I have a bash string 20220416124334 (the string is not epoch) and I want to convert it to date format so it should look like this: 2022/04/16 12:43:34
I've tried:
[manjaro#manjaro ~]$ date -d '20220416124334' "+%Y/%m/%d %H:%M:%S"
date: invalid date ‘20220416124334’
How should I do it correctly ?
I would expect to use the date command, as you employ it, with a number that represents the number of seconds since the epoch. It seems your number is a formatted date yyyymmddhhmiss.
If you don't need to validate the string that represents a formatted date, then you can use sed to insert extra formatting characters:
echo '20220416124334' | sed -E 's/(....)(..)(..)(..)(..)(..)/\1\/\2\/\3 \4:\5:\6/'
If you end up taking as input a number of seconds since the epoch, then do it this way, keeping in mind that a number means different times in different time zones at different times of the year (eyeroll):
date -u -r 1650113014 "+%Y/%m/%d %H:%M:%S"

Sum number of days to specific date in dd/mm/yyyy format in bash

This might be a simple problem but I'm having some trouble getting trough it:
I need to sum a number of weeks to a specific date that is in the format dd/mm/yyyy.
I know I can sum number of weeks to the current day using date -d "+1 week" +"%d%m%Y", but I'm having trouble when I try to do this to a specific date in the format I mentioned above (29/06/2019, for example).
Would anyone be able to explain how I can perform this operation?
$ date -d '20190629+1 week' +"%d%m%Y"
06072019
$ date -d '06/29/2019+1 week' +"%d%m%Y"
06072019
without delimiters it's not a valid date format though.

How to extract the UTC offset of a specific date and time using bash?

To be specific I do not want date +%z or date -R for the current local time, but I need something very similar that should work for any date in the past. For example, something like:
command 2018-04-01 12:33:45
should return the UTC offset (assume Chicago as my local time zone) at 12:33:45 on 1st April 2018 local time, Chicago time for example.
I searched extensively and there, probably, is no question close to this one, everyone wants current offset not date-time specific one, therefore it is not a duplicate.
Thanks very much
Update:
I have found something here, that asnwers how to get past dates using date command, then I have combined it with -R to get something close to what I want:
date -d "35 days ago" -R
I can go 35 days back and get the UTC offset.
To convert a particular date time to UTC you can use below command
for example date you provided in your question
2018-04-01 12:33:45
It would be something like below
date -u --date=#$(date "+%s" --date="2018-04-01 12:33:45")
which would have output similar to Sun Apr 1 10:33:45 UTC 2018
If you would like to achieve command date here then you can either create a command alias for above command or use above command in your script providing the date value to convert as an argument
I am answering my own question based on
My own update just after submitting the question.
Nahuel Fouilleul's comment.
Quoting from my own "edit":
"I have found something here, that answers how to get past dates using date command, then I have combined it with -R to get something close to what I want:
date -d "35 days ago" -R
I can go 35 days back and get the UTC offset."
here comes Nahuel's comment, "what about date -d 2018-04-01T12:33:45 -R"
That is in line with what I wrote in my "edit". Nahuel's solution works perfectly for me.
also Usman Malik's answer perfectly provides the solution.
So to summarize, my (not entirely though) answer will be:
date -d specific_past_date -R
where the specific_past_date is the date on which I need the UTC offset, using the date and time that I mentioned in my question, if I do:
date -d 2018-04-01T12:33:45 -R
I get Sun, 01 Apr 2018 12:33:45 -0500 from Chicago, that means Chicago time was 5 hours behind UTC on 1st April 2018 at 12:33:45.

Get Current month using `date` in Linux

I am trying to get current month based on date.
Till now it was working fine with below command -
cur_mon=`date --date="$report_date" '+%m' -d 'now'`
cur_year=`date --date="$report_date" '+%y' -d 'now'`
echo cur_mon and cur_year =${cur_mon} and ${cur_year}
$report_date is now 30th Sep 2014.
It is giving me output -
cur_mon and cur_year =10 and 14
Let me know if anyone knows the reason as well solution.
Also this can also create problem in year. If date is 31st Dec, 2014.
I think I see your problem, you are trying to describe your date twice. Once with --date="$report_date" and second time with -d now. Only the latter one is in effect. That's why you see 10 instead of 9.
Try awk '{print $2}' <(date) it should select the second element delimited by spaces in the output of date which is nominally month.

how to get day of the year in shell?

How can I get the day of the year in shell?
date '+%V' will give me the week of the year, which is 15 for today; but I need to find the day of the year!
From the coreutils date manual:
%j day of year (001..366)
Use the date command and the %j option...
doy=$(date +%j)
POSIX mandates the format string %j for getting the day of the year, so if you want it for today's date, you are done, and have a portable solution.
date +%j
For getting the day number of an arbitrary date, the situation is somewhat more complex.
On Linux, you will usually have GNU date, which lets you query for an arbitrary date with the -d option.
date -d '1970-04-01' +%j
The argument to -d can be a fairly free-form expression, including relative times like "3 weeks ago".
date -d "3 weeks ago" +%j
On BSD-like platforms, including MacOS, the mechanism for specifying a date to format is different. You can ask it to format a date with -j and specify the date as an argument (not an option), and optionally specify how the string argument should be parsed with -f.
date -j 04010000 +%j
displays the day number for April 1st 00:00.
The string argument to specify which date to examine is rather weird, and requires the minutes to be specified, and then optionally allows you to prefix with ((month,) day, and) hour, and optionally allows year as a suffix (sic).
date -j -f "%Y-%m-%d" 1970-04-01 +%j
uses -f format date to pass in a date in a more standard format, and prints the day number of that.
There's also the -v option which allows you to specify relative times.
date -j -v -3w +%j
displays the day number of the date three weeks ago.
If you are looking for a proper POSIX-portable solution for getting the day number of arbitrary dates, the least unattractive solution might be to create your own program. If you can rely on Python or Perl (or GNU Awk) to be installed, those make it relatively easy, though it's still a bit of a chore using only their default libraries.

Resources