[Solved] Previous Year Date


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Previous Year Date
# 1  
Old 04-22-2013
[Solved] Previous Year Date

Hi Gurus,

I would like to get the date for the previous year based on the date I specify.
For e.g. If I input today's date (i.e. 20130422) I need to get 20120422.

We don't have GNU and use K Shell. Any help is highly appreciated.

Thanks
Shash
# 2  
Old 04-22-2013
Hi, try this
Code:
X=20130422
Y=$(echo $X|cut -c1-4)
M=$(echo $X|cut -c5-6)
D=$(echo $X|cut -c7-8)
Y1=$(( Y - 1 ))
if [[ $M -eq 2 ]] && [[ $D -eq 29 ]]
then
D=28
fi
echo ${Y1}${M}${D}

# 3  
Old 04-22-2013
Here is another way to do this just using Korn shell built-ins (and doing minimal error checking):
Code:
#!/bin/ksh
if [ $# -ne 1 ] || [[ $1 != [0-9][0-9][0-9][0-9][01][0-9][0-3][0-9] ]]
then    printf "Usage: %s YYYYMMDD\n" "${0##*/}" >&2
        exit 1
fi
y="$((${1%????} - 1))"
md="${1#????}"
if [ "$md" = 0429 ]
then    md=0428
fi
echo $y$md

# 4  
Old 04-22-2013
Code:
#!/bin/ksh93

$ date="20130422"
$ printf "%(%Y%m%d)T\n" "$date 12 months ago"

Produces 20120422
# 5  
Old 04-23-2013
The solution given by franzpizzo worked for me. Thanks a lot for franzpizzo & others.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How bash treats literal date value and retrieve year, month and date?

Hi, I am trying to add few (say 3 days) to sysdate using - date -d '+ 3 days' +%y%m%d and it works as expected. But how to add few (say 3 days) to a literal date value and how bash treats a literal value as a date. Can we say just like in ORACLE TO_DATE that my given literal date value... (2 Replies)
Discussion started by: pointers1234
2 Replies

2. Shell Programming and Scripting

Not able to fetch previous month first date and last date

I am not able to fetch first date and last date previous month date -d -1month +%Y-%m-%d date -d -1month +%Y-%m-%d I need two format dd-mm-yyy previous month 01-03-2016 previous month 31-03-2016 and also only date 1 to 31 Aprriciate your replay (4 Replies)
Discussion started by: jagu
4 Replies

3. UNIX for Dummies Questions & Answers

Command to print previous year in UNIX

hi all, I use date +%Y which gives Current year. Requirement: I want previous year to be printed. Please help me. Note: I tried date +%d/%m/%Y -d "-1 years" which is not working. (10 Replies)
Discussion started by: wasim999
10 Replies

4. Shell Programming and Scripting

Find week of the year for given date using date command inside awk

Hi all, Need an urgent help on the below scenario. script: awk -F"," 'BEGIN { #some variable assignment} { #some calculation and put values in array} END { year=#getting it from array and assume this will be 2014 month=#getting it from array and this will be 05 date=#... (7 Replies)
Discussion started by: vijaidhas
7 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Using date command, getting previous day

Legends, i need to get previous day using date command. Can you please help. sdosanjh:/home> date +%m%d%y 011514 i tried -d '-1 day' but it is not working (5 Replies)
Discussion started by: sdosanjh
5 Replies

6. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

7. Shell Programming and Scripting

finding the previous day date and creating a file with date

Hi guys, I had a scenario... 1. I had to get the previous days date in yyyymmdd format 2. i had to create a file with Date inthe format yyyymmdd.txt format both are different thanks guys in advance.. (4 Replies)
Discussion started by: apple2685
4 Replies

8. Shell Programming and Scripting

want to get previous date from date command in ksh

I want to get previous date from date command. I am using ksh shell. Exmp: today is 2008.09.04 I want the result : 2008.09.03 Please help. Thanks in advance. (4 Replies)
Discussion started by: rinku
4 Replies

9. Shell Programming and Scripting

Specify a previous date as start date in shell script

Hi, I am trying to modify a script which accepts date in format dd/mm/yy. I am trying to modify the script so that it retrieves the date that was 15 days earlier from today as start date. Eg.if today is 05/09/2006, the script should retrieve 21/08/2006 as start date. Is there any script/code to... (2 Replies)
Discussion started by: ritzwan0
2 Replies
Login or Register to Ask a Question