[ksh] Weekday a week ago


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [ksh] Weekday a week ago
# 1  
Old 01-10-2013
[ksh] Weekday a week ago

Hi,

Using TZ and date gives an easy way to find out the dates one or more days ago.
Now I am in need of knowing the date one week ago.
So, now is Thursday and I want to know the date on Thursday a week ago.
Unfortunately I can only get as far back as 6 days ago
When using TZ+168 it reverts back to today.

Code:
# i=24
# LOG_DATE=`TZ=GMT+$i date "+%a %d %b"`
# echo $LOG_DATE
Wed Jan 09
# i=144
# LOG_DATE=`TZ=GMT+$i date "+%a %d %b"`
# echo $LOG_DATE
Thu Jan 04
# i=168
# LOG_DATE=`TZ=GMT+$i date "+%a %d %b"`
# echo $LOG_DATE
Thu Jan 10

Any thoughts on this ?

Thanks,

ejdv
# 2  
Old 01-10-2013
Code:
 date --date="last Thursday" +'%a %b %d'
Thu Jan 03

# 3  
Old 01-10-2013
If you like to use only ksh builtin without external gnu date command, then
Code:
#!/bin/ksh93
today=$(printf "%(%#)T" now   )   # epoc  
# or today=$(printf "%(%s)T" now   ) 
(( week=60*60*24*7 ))
(( prevweek=today-week ))
# convert int to datestr, attributes is same as in date command
printf "%(%a %d %b)T\n" "#$prevweek"

# 4  
Old 01-10-2013
This works for modern versions of the Korn shell
Code:
$ printf %T now
Thu Jan 10 17:52:00 PST 2013
$ printf %T "7 days ago"
Thu Jan  3 17:52:35 PST 2013
$

# 5  
Old 01-10-2013
I also suggest you to have a look at this thread created by Perderabo. I hope this will be really helpful.
# 6  
Old 01-11-2013
hi this can also work
Code:
$ printf %t now
Thu Jan 10 17:52:00 PST 2013
$ printf %t "7 days ago"
Thu Jan  3 17:52:35 PST 2013
$

# 7  
Old 01-11-2013
Thanks for all the replies.
But none of them work :-(

Perhaps I had to tell that I am using Solaris 10 as OS.
The .profile sets the shell to ksh.
In the scripts I always use "#!/bin/ksh".
Looks it isn't a 'modern' version of ksh.

Code:
# printf %t now
t#

Code:
# printf %T now
T#

Code:
# date --date="last Thursday" +'%a %b %d'
date: illegal option -- date=last Thursday
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]

Code:
# printf "%(%#)T" now
(#

Looks like I need a short escape to perl for this:

Code:
# perl -e 'print localtime(time() - 604800) . "\n" '
Fri Jan  4 10:15:36 2013

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 to find a file that's modified more than 2 days ago but less than 5 days ago?

How to find a file that's modified more than 2 days ago but was modified less than 5 days ago by use of any Linux utility ? (4 Replies)
Discussion started by: abdulbadii
4 Replies

2. Shell Programming and Scripting

How to get the consecutive last 10 week day date using UNIX ksh shell scripting?

Hi, i am writing a ksh shell script to check the last month end date whether it is falling in last 10 week day date, I am not sure How to use "Mr. Perderabo's date calculator", Could you Please let me know how to use to get my requirement, I tried my own script but duplicate week day and... (5 Replies)
Discussion started by: karthikram
5 Replies

3. Shell Programming and Scripting

yesterday's "weekday" date

i've been going through https://www.unix.com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html to find a cmd that will print me yesterday's date. I have found one that does it nicely set YEST = `date '+20%y/%m/%d' | awk -F"/" '{print $1$2($3-1)}'` as you can... (3 Replies)
Discussion started by: jack.bauer
3 Replies

4. Shell Programming and Scripting

Writing a script to run weekly/monthly - check for weekday or day-of-the-month

Hi all, I currently have a UNIX file maintenance script that runs daily as a cron job. Now I want to change the script and create functions/sub inside it that runs on a weekly or monthly basis. To run all the scripts' daily maintenance, I want to schedule it in cron as simply maint.sh... (1 Reply)
Discussion started by: newbie_01
1 Replies

5. Shell Programming and Scripting

get weekday based on date

Hi all, i am looking for a method of determining the weekday when date is know (bash, if possible). Let's say that i am looking to get the weekday for MAY 01 2011, how can i convert this into Sunday or SUN? any suggestions? (4 Replies)
Discussion started by: gigagigosu
4 Replies

6. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

7. Shell Programming and Scripting

How to get the weekday of the given date?

Hi All, Thanks in Advance. I want a function/script which returns the weekday of the given date. the input to the function/script is the date with format MM/DD/YYYY, it should return the weekday as 1 for sunday, 2 for monday .......7 for saturday. ex: if the function called like this ... (14 Replies)
Discussion started by: rinku11
14 Replies

8. Shell Programming and Scripting

How to identify week-1 and week-2

Hello Friends I have three dirs 1. /home/main-bkup 2. /home/bkup-week1 3. /home/bkup-week2 Now we copy backups in 1 initially, then on 1st week we copy few content of 1 into 2 and then run some scripts on that. Then in 2nd week we keep 2 untouched and do the same thing in 3. So I... (1 Reply)
Discussion started by: csaha
1 Replies

9. UNIX for Dummies Questions & Answers

[kornshell] Getting the next weekday date

Hi All can anyone help me with this, Im new to kornshell scripting and is trying to get the next weekday to a variable: strDate=%date '+%Y%m%d' // YYYYMMDD strNewDate= :confused: // assuming that current date is 20050812 (friday) then strNewDate will get 20050815 (monday) or if... (1 Reply)
Discussion started by: rs_f01
1 Replies
Login or Register to Ask a Question