I required previous month and year value in a variable in Unix . I am not able to find any correct unix command for that.
Meanwhile I have achieved this requirement through sql command, in ext variable and used it.
ext=`sqlplus -s user/pass <<END
set feedback off set pagesize 0
select to_char(add_months(sysdate,-1),'Mon-YYYY') from dual;
exit;
END>>`
But I was looking if I can achieve this by unix command in which I can get same result as Aug-2016.
Most close I have got through unix command is by
ext=date + %b-%Y
which is giving me current month year value as Sep-2016.
But I require previous month value and in same format.Can anyone suggest? Thanks.
Have you tried date -d ?
% date -d '1 month ago' '+%b-%Y'
Aug-2016
Related
I need to know if there is a command where I can show what time it will be after X seconds.
In my case: I often need to set "sleep (seconds)"; in terminal before and between other following commands (seperated by ;) and it would be greatfull to see what time it will be after (seconds).
I know this command: date -d#(seconds) -u +%H:%M:%S. But this only show me the duration, so its a conversion. That is a compromise but not exactly what I need.
I hope I explained it understandable and someone can help me^^
Best regards and thanks in advance
You can do a sum to current timestamp (in second) with the coming sleep delay (in seconds too) with $((a + b)).
Then, your initial command transforms to:
date -d#$((timestamp + 10)) -u +%H:%M:%S
The final command can look like:
date -d#$((`date +%s` + 60)) -u +%H:%M:%S
EDIT: As per the OP comment bellow this answer, "Why do I have to add 3600 for not having a timestamp in the past?"
This is due to the local of your system. Indeed, in one part of the command you get the total elapsed seconds from 1970 ... regarding your local, while on the other part, you print the timestamp in reference to UTC.
As your local may be UTC+1 this explains why you have to add one more extra hour (3600 seconds).
You can stick to your system's local with:
date -d#$((`date +%s` + 60)) +%H:%M:%S
This question already has answers here:
How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?
(30 answers)
Closed 1 year ago.
i am using batch and i need help formatting this one thing, on saved file the date changes everyday but the "yyyy" is just put down as "yy"
Example: if the year was "2021" the name would be "21"
I havent found anything that could get me the last 2 numbers on the year.
Please help as i need this for work.
Sincerely,
Kythe
You might cut out the part you do not need:
set DT=%date%
set dd=%DT:~0,2%
set mm=%DT:~3,2%
set yy=%DT:~8,2%
echo %dd%
echo %mm%
echo %yy%
echo "day: %dd%, month: %mm%, year: %yy%"
pause
set variable=%date:~start,length% assigns to the variable all the the characters of date indexed between start (counting from 0) and (start + length-1)
EDIT:
On other machine date might be formatted differently, as #Squashman pointed out.
To properly cut out the part you need, try:
echo %date%
Now set dd, mm and yy in the correct way you need them to be set
I know the topic has already emerged and some of the posts give a good summary like the one here: Convert string to date in bash . Nevertheless, I encounter a problem presented below with an example I should solve:
date +'%d.%m.%y' works as desired and returns 05.12.20 but the inverse operation I should use to convert strings to date fails:
date -d "05.12.20" +'%d.%m.%y'
date: invalid date ‘05.12.20’
and this is exactly what I need. The Unix date formatting I have also checked on https://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/ but it seems to be in line with that. What is the problem? I also tried to supply time zone indicators like CEST but they did not solve the problem.
Try
date -d "05-12-20" +'%d.%m.%y'
UNIX date expects either - or / as a date separator.
However, if your input really must be in the format "05.12.20" (i.e. using .), then you can convert it to the format expected by UNIX date:
date -d `echo "05.12.20" | sed 's/\./-/g'` +'%d.%m.%y'
I am working in a software program called PastPerfect that has a "command window" where it says you can use dbase commands to do global updates to the program's dbf files.
THE PROBLEM: a user accidentally entered the wrong year, "1901", in a date field across multitudes of records and it needs to be replaced/fixed with the year "2001".
I have tried:
REPLACE YEAR(catdate) WITH 2001 FOR YEAR(catdate)=1901
and it keeps telling me it is an Invalid Command
Can somebody give me the correct dbase/foxpro syntax to replace all the years that are 1901 with 2001?
The syntax for the REPLACE command is.
REPLACE FieldName WITH Value FOR BooleanExpression
If CATDATE is a date field (no time), then
REPLACE catdate WITH DATE(2001, MONTH(catdate), DAY(catdate)) FOR YEAR(catdate) = 1901
If CATDATE is a date time field, then
REPLACE catdate WITH DATETIME(2001, MONTH(catdate), DAY(catdate), HOUR(catdate), MINUTE(catdate), SEC(catdate)) FOR YEAR(catdate) = 1901
What's the quickest way to convert a date in one format, say
2008-06-01
to a date in another format, say
Sun 1st June 2008
The important bit is actually the 'Sun' because depending on the dayname, I may need to fiddle other things around - in a non-deterministic fashion. I'm running GNU bash, version 3.2.17(1)-release (i386-apple-darwin9.0).
[Background: The reason that I want to do it from the command line, is that what I really want is to write it into a TextMate command... It's an annoying task I have to do all the time in textMate.]
$ date -d '2005-06-30' +'%a %F'
Thu 2005-06-30
See man date for other format options.
This option is available on Linux, but not on Darwin. In Darwin, you can use the following syntax instead:
date -j -f "%Y-%m-%d" 2006-06-30 +"%a %F"
The -f argument specifies the input format and the + argument specifies the output format.
As pointed out by another poster below, you would be wise to use %u (numeric day of week) rather than %a to avoid localization issues.
Reading the date(1) manpage would have revealed:
-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 another.
Thanks for that sgm. So just so I can come back to refer to it -
date -j -f "%Y-%m-%d" "2008-01-03" +"%a%e %b %Y"
^ ^ ^
parse using | output using
this format | this format
|
date expressed in
parsing format
Thu 3 Jan 2008
Thanks.
date -d yyyy-mm-dd
If you want more control over formatting, you can also add it like this:
date -d yyyy-mm-dd +%a
to just get the Sun part that you say you want.
date -d ...
doesn't seem to cut it, as I get a usage error:
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
I'm running GNU bash, version 3.2.17(1)-release (i386-apple-darwin9.0), and as far as the man goes, date -d is just for
-d dst Set the kernel's value for daylight saving time. If dst is non-
zero, future calls to gettimeofday(2) will return a non-zero for
tz_dsttime.
If you're just looking to get the day of the week, don't try to match strings. That breaks when the locale changes. The %u format give you the day number:
$ date -j -f "%Y-%m-%d" "2008-01-03" +"%u"
4
And indeed, that was a Thursday. You might use that number to index into an array you have in your program, or just use the number itself.
See the date and strftime man pages for more details. The date manpage on OS X is the wrong one, though, since it doesn't list these options that work.