I want to get the previous date in AIX environment.
But it seems date -d commands can't work. And I only found
yesterday=TZ=aaa24 date +%Y%m%d as solution.
However, this raise me the timezone problem and I don't know how to solve it.
And I tried these commands, but it gives wrong date in some time period. (e.g. today is 20190418 07:01:19, yesterday will be 20190416 23:01:19)
today=`date +%Y%m%d`
yesterday=`TZ=aaa24 date +%Y%m%d`
yesterday=`TZ=$TZ+24 date +%Y%m%d`
I echo my #TZ and it is HKG-8.I know that I can use +16 to force this become my answer. But it's not work if the timezone is changed.
Can anyone suggest any solutions for get previous date of a date value or simply any ways to solve time zone problems?
This sample script can help you:
date +%s|awk '{printf "%s\n", strftime("%c",$1-86400)}'
Related
Is there a way to return the creation date of a file in the following format?
yyyymmdd_hhmmss (ie 20230124_181920)
Many thanks for any help
With GNU's date and stat command:
date -d#"$(stat -c %W file)" '+%Y%m%d_%H%M%S'
As people pointed out, availability of creation date info depends on [file]system.
I‘m aware of date +%u to get the day of the week for today.
I‘d like to get that integer for any arbitrary date i input - if possible in the format I choose (e.g. %YYmmdd)
ok, found it finally:
date -j -f %Y%m%d +%u 20200910
this is, because date on macOS doesn't take a switch for putting in custom date (fyi for those folks, how try to make -v work, like me^^)
in addition, -f affects only input format (it's literally the second word in the man page, but I managed to overlook more than once)
-j is needed to use -f without setting the date.
hope this will spare someone time in the future ;)
edit:
it seems to be important, to specify input format before output format (see comment from #chepner below)
(also be careful with quotes)
$ date +%u -d "2020-09-10"
4
I want to print the day of the last Monday for example
using the date command in a terminal
Provided I do not know what day/ date is today but I can use date to find out the date and then use it to find out what date the first previous monday was.
If you are using gnu date, you can try the following command:
date -d "last monday"
I have a shell script where I ask the system to return the year, 660days ago. On my Mac, I use this:
date -j -v-660d +"%Y"
If run today, that would return 2011.
I'm moving the script to an Ubuntu machine, and I receive an error, stating that the -j and -v options are invalid.
I've looked at the man page, looking for equivalent options, but haven't been able to find a solution.
Any help is appreciated.
This should work:
$ date -d '660 days ago' +%Y
2011
Sometimes, there are more than one version of utilities like date. Oftentimes, the version shipped on Linux is not the same as the version shipped on BSD or macOS.
I found the man page for date on FreeBSD, and it says this:
-j Do not try to set the date. This allows you to use the -f flag
in addition to the + option to convert one date format to an-
other. Note that any date or time components unspecified by the
-f format string take their values from the current time.
-v [+|-]val[y|m|w|d|H|M|S]
Adjust (i.e., take the current date and display the result of the
adjustment; not actually set the date) the second, minute, hour,
month day, week day, month or year according to val. If val is
preceded with a plus or minus sign, the date is adjusted forwards
or backwards according to the remaining string, otherwise the
relevant part of the date is set. The date can be adjusted as
many times as required using these flags. Flags are processed in
the order given.
For the GNU version of date shipped on Linux, typically, the only way to set the date is to pass -s. So we don't need the -j flag. The GNU version of date has -d display the date describe by a passed string.
In your case, you want:
$ date -d '660 days ago' +%Y
2021
Is there a way to add date in the name of the file... we can add current date in this manner date '+%Y%m%d' but i want to add "filename_date_1-2-2011_thru_31-2-2011.txt" Is it possible to do that??????????
If you have a sufficiently advanced version of the date command and you know a Unix timestamp for the start and end dates, then you can use:
(MacOS X) date -r 1234567890 "+%d-%m-%Y" to obtain 13-02-2009.
(GNU) date -d 2/13/2009 "+%d-%m-%Y" to obtain 13-02-2009 again.
If you don't want the leading zeroes on the day of month, then you need to use '%e` instead of '%d' on Linux (but that puts a space in place of the zero). It is not clear that there's a format specifier for day-of-month without a leading zero on MacOS X; nor is it clear that there's a way to format month of year as a single-digit number for January to September on either platform.
You get the format into your C shell script using back-ticks around the date commands.
Consider reading Csh Programming Considered Harmful and heeding its advice.