- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Can I get yesterday's date in a unix script?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-19-2004 04:13 AM
тАО04-19-2004 04:13 AM
Is there a relatively painless way of getting yesterday's date into a variable? (i.e. variable_name2=??????????)
Thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-19-2004 04:16 AM
тАО04-19-2004 04:16 AM
Re: Can I get yesterday's date in a unix script?
file=`TZ=$TZ+24 date "+%y%m%d"`
thanks Chris..
Check this link.
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=222442HTH,
Gideon
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-19-2004 04:19 AM
тАО04-19-2004 04:19 AM
Re: Can I get yesterday's date in a unix script?
The most foolproof way is to use Perl's date command subtracting 86400 from the current time or simply use this script which can do much more than just 1 day forward or backwards.
YESTERDAY=$(caljd.sh -y -s $(caljd.sh -p 1))
echo "Yesterday = ${YESTERDAY}"
Invoke as caljd.sh -u for full usage and examples.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-19-2004 04:19 AM
тАО04-19-2004 04:19 AM
Re: Can I get yesterday's date in a unix script?
From the FAQ section on my site:
--8<---
How do I print Yesterday?
d3:/ 102 > echo 'Yesterday'
Yesterday
d3:/ 103 > perl -le 'print scalar localtime time - 86400'
Or use A.Clay Stephenson's date hammer, which can also be used for more complicated actions from the command line, as will using the modules Date::Calc or Date::Manip with Perl.
-->8---
Clay's date hammer will be mentioned a lot when you search for this question on the forum search pages. It is also available from my site
fiddling with timezone's helps (sometimes)
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-19-2004 04:20 AM
тАО04-19-2004 04:20 AM
Re: Can I get yesterday's date in a unix script?
From the FAQ section on my site:
--8<---
How do I print Yesterday?
d3:/ 102 > echo 'Yesterday'
Yesterday
d3:/ 103 > perl -le 'print scalar localtime time - 86400'
Or use A.Clay Stephenson's date hammer, which can also be used for more complicated actions from the command line, as will using the modules Date::Calc or Date::Manip with Perl.
-->8---
Clay's date hammer will be mentioned a lot when you search for this question on the forum search pages. It is also available from my site
fiddling with timezone's helps (sometimes)
My HP ITRC site pages can be found at
Singapore https://www.beepz.com/personal/merijn/
Rotterdam http://www.cmve.net/~merijn/
Seattle http://www.hpux.ws/merijn/
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-19-2004 04:42 AM
тАО04-19-2004 04:42 AM
Re: Can I get yesterday's date in a unix script?
since you were the first to reply I gave your example the first shot.
Here's my code:
today=`date +%Y%m%d`
yesterday=`TZ=$TZ+24 date "+%Y%m%d"`
echo "today is " $today
echo "yesterday is " $yesterday
When I execute it, here are my results:
today is 20040419
yesterday is 20040419
I also tried it with "-24" and by taking the "+24" out altogether, all with the same results.
(I tried taking a look at the link you provided, but it's down at the moment.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-19-2004 05:36 AM
тАО04-19-2004 05:36 AM
Re: Can I get yesterday's date in a unix script?
Use a more robust techique, Perl, caljd.sh or the Gnu date command.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-19-2004 08:15 AM
тАО04-19-2004 08:15 AM
SolutionIf you insist on using TZ -
TZ=PST8PDT+24 date +%Y%m%d | read Yesterday
Where PST8PDT is the offset which may be different in your locale, simply substitue with your offset.
Best regards,
dl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-19-2004 08:45 AM
тАО04-19-2004 08:45 AM
Re: Can I get yesterday's date in a unix script?
It's not that I insist on using TZ. I tried downloading A. Clay's caljd.sh, but couldn't get it to work.
Since I'm using this basically for display purposes anyway, it's not a big deal if it hiccups once in a while.
(I'm closing this discussion.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-19-2004 09:13 AM
тАО04-19-2004 09:13 AM
Re: Can I get yesterday's date in a unix script?
I know you said that this was closed but I just tried the version of caljd.sh that Clay attached and it worked just as expected. The only trouble that I have ever had with his script was when I tried to run it on Solaris. I just changed #!/usr/bin/sh to #!/usr/bin/ksh to use the korn shell and it worked perfectly. You should really learn how to use this script. It will solve almost any date problem you can think of.
Regards,
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-19-2004 02:52 PM
тАО04-19-2004 02:52 PM
Re: Can I get yesterday's date in a unix script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-19-2004 05:41 PM
тАО04-19-2004 05:41 PM
Re: Can I get yesterday's date in a unix script?
Hope this script will help
#!/bin/ksh
### Script to get Yesterday Date #############
Y=`date +'%Y'`
M=`date +'%m'`
D=`date +'%d'`
today=$Y$M$D
echo $today
yesterday=$Y$M`expr $D - 1`
echo $yesterday
Regards,
Santosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-20-2004 01:05 AM
тАО04-20-2004 01:05 AM
Re: Can I get yesterday's date in a unix script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-20-2004 02:19 AM
тАО04-20-2004 02:19 AM
Re: Can I get yesterday's date in a unix script?
This is the most unreliable piece of date calculation I've ever seen here on the forum.
Ever considered what happens on every first day of each month?
No month rolovers, no year rollovers, no check for valid dates.
Please do not you it in production work, unless you actually don't care that "yesterday" might e invalid
Enjoy, Have FUN! H.Merijn [ who advises to get caljd running ! ]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-20-2004 09:46 PM
тАО04-20-2004 09:46 PM
Re: Can I get yesterday's date in a unix script?
Thanks for Pointing out that. I am sorry if you have something better than this please share it with the fourm.
Hi Dick,
This New Script takes care of Month Rollover , hope this will be useful to you now.
Cheers
Santosh
#!/bin/ksh
### Script to get Yesterday Date #############
Y=`date +'%Y'`
M=`date +'%m'`
D=`date +'%d'`
today=$Y$M$D
echo $today
if [ $M -eq 01 -o $M -eq 03 -o $M -eq 05 -o $M -eq 07 -o $M -eq 08 -o $M -eq 10 -o $M -eq 12 ] && [ $D -eq 01 ];then
yesterday=$Y$M`expr $D + 30`
echo $yesterday
else
if [ $M -eq 04 -o $M -eq 06 -o $M -eq 09 -o $M -eq 11 ] && [ $D -eq 01 ]; then
yesterday=$Y$M`expr $D + 29`
echo $yesterday
else
if [ $M -eq 02 -a $D -eq 01 ]; then
yesterday=$Y$M`expr $D + 27`
echo $yesterday
else
yesterday=$Y$M`expr $D - 1`
echo $yesterday
fi
fi
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-20-2004 10:00 PM
тАО04-20-2004 10:00 PM
Re: Can I get yesterday's date in a unix script?
lt09:/home/merijn 103 > perl -le 'print scalar localtime time - 86400'
Tue Apr 20 12:16:45 2004
lt09:/home/merijn 104 > perl -e'@x=localtime time-86400;printf"%4d%02d%02d\n",$x[5]+1900,$x[4]+1,$x[3]'
20040420
lt09:/home/merijn 105 >
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-21-2004 08:30 AM
тАО04-21-2004 08:30 AM
Re: Can I get yesterday's date in a unix script?
You still have a bug. Your code doesn't handle leap years --- and remember century years are not leap years unless they are divisible by 400.
Date calculations are always a pain.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-17-2006 07:03 AM
тАО07-17-2006 07:03 AM
Re: Can I get yesterday's date in a unix script?
I read your question and thought you may be able to pass the advice on the date question I have listed below. I tried to calculate n22edate which go back 375/675 days from the date I entered but get the wrong date calculation
read sdate?"Enter first day of report period (YYYYMMDD) "
read rdate?"Enter last day of report period (YYYYMMDD) "
export n22edate1=`expr $sdate - 375`
export n22sdate1=`expr $rdate - 675`
echo n22edate1 $n22edate1
echo n22sdate1 $n22sdate1
export n22edate=`echo "$n22edate1" | cut -c 1-6`"31"
export n22sdate=`echo "$n22sdate1" | cut -c 1-6`"01"
echo n22edate $n22edate
echo n22sdate $n22sdate
**********OUTPUT*********'
Enter first day of report period (YYYYMMDD) 20040810
Enter last day of report period (YYYYMMDD) 20050531
mo 05
yr 05
n22edate1 20040435
n22sdate1 20049856
n22edate 20040431
n22sdate 20049801
It's most appreciated if you are willing to advice.
Thanks,
Sara
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-17-2006 07:28 AM
тАО07-17-2006 07:28 AM
Re: Can I get yesterday's date in a unix script?
#!/usr/bin/sh
OFFSET1=375
echo "Enter 1st date of report period (YYYY MM DD): \c"
read YR MO DAY
DT1=$(caljd.sh -y $(caljd.sh -y -p ${OFFSET1} ${YR} ${MO} ${DAY}))
echo "${OFFSET1} days before was ${DT1}"
Make sure the attached script, caljd.sh is somewhere in your path and make it executable.
Invoke as caljd.sh -u for usage and many examples.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-01-2006 04:07 AM
тАО09-01-2006 04:07 AM
Re: Can I get yesterday's date in a unix script?
Hi
I need to extend this script to be able to pass a parameter of 1, or 2 or 3 days(i.e. yesterday, the day before etc). Can the author of this script or some body else
help?
Thanks
P.S. I tried to use the TZ script and caljd.sh. TZ worked only for yesterday. Caljd.sh didn't work at all.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-01-2006 04:12 AM
тАО09-01-2006 04:12 AM
Re: Can I get yesterday's date in a unix script?
caljd.sh -u
will give full usage instructions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-01-2006 04:20 AM
тАО09-01-2006 04:20 AM
Re: Can I get yesterday's date in a unix script?
Pasted from telnet session:
[prissy]/home/cstephen: P3=$(caljd.sh -y -S "/" $(caljd.pl -p 3))
[prissy]/home/cstephen: echo "${P3}"
2006/08/29
[prissy]/home/cstephen:
It displays 3 days before now (1-Sep).
Vrsn 2.27 is the latest version and it is attached just above your last posting. I suspect, what you are not telling me is that you are running a non POSIX/non Korn shell (bash?). If you are on Linux, run it under zsh.
In any event, the Perl script just needs to change 86400 to some integer multiple (negative or positive) to adjust the time to days before or days after the current date.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-01-2006 05:34 AM
тАО09-01-2006 05:34 AM
Re: Can I get yesterday's date in a unix script?
caljd.sh is a large shell that needs to be saved some place and called from my script. My shop is very small and there's nobody to maintain it if I am not here. However, it is extremely flexible. When I tried to copy and paste in a 'vi' session, I had problem with the size of vi buffer. However, I saved and copied the file and made it work. The perl script can be incorporated in my script. I only need to pass a parameter that will have only 1, 2 or 3. I don't know how to pass a parameter to perl. Hence the post. You both converted me to use caljd.sh
Thanks a lot. Problem solved.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-01-2006 05:46 AM
тАО09-01-2006 05:46 AM
Re: Can I get yesterday's date in a unix script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-03-2006 02:16 PM
тАО09-03-2006 02:16 PM
Re: Can I get yesterday's date in a unix script?
simply type this:
DTE=$(date +%Y%m%d -1)
((Y=DTE - 1))
print $Y
20060902
print $DTE
20060903
Done...its as simple as that!