- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: date manipulating
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
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
тАО10-09-2001 12:19 PM
тАО10-09-2001 12:19 PM
Solved! Go to Solution.
- Tags:
- date arithmetic
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-09-2001 12:29 PM
тАО10-09-2001 12:29 PM
Re: date manipulating
There a number of ways you can do it
1. YD=`TZ=aaa24 date +%Y%m%d`
this will store yesterdays date in YD in the format YYMMDD
2.Yesterday=`TZ=PST+24 date +%D`
this will give it in the format 10/08/01
3. export YD=$(perl -e 'print scalar localtime(time-1*86400),"\n"' |cut -c 5-11)
this will just store Oct 8 in YD variable.
-HTH
Ramesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-09-2001 12:29 PM
тАО10-09-2001 12:29 PM
Re: date manipulating
http://forums.itrc.hp.com/cm/QuestionAnswer/1,11866,0x06ed660142b2d5118ff10090279cd0f9,00.html
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-09-2001 12:30 PM
тАО10-09-2001 12:30 PM
Re: date manipulating
echo $(sh -c "TZ=$(date +%Z)+24; export TZ; date '+%m%d%y'")
See this URL for more info:
http://www.dutchworks.nl/htbin/hpsysadmin?h=3&dn=19828&q=date%20script&fh
-Santosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-09-2001 12:36 PM
тАО10-09-2001 12:36 PM
Re: date manipulating
I'll give you my standard answer. I always use Julian days to do this - astromers use this method so that leap years, months, ... don't make calculations messy. If you call the attached script with no args, it converts today's date into a Julian Day (~ number of days since 4004 BCE); subtract 1 from this and feed that number in and it returns a calendar date. This method will work across any number of days past or future.
e.g.
#!/usr/bin/sh
TODAYJDATE=`caljd.sh`
PREVJDATE=$(( ${TODAYJDATE} - 1))
PREVDATE=`caljd.sh ${PREVJDATE}`
echo "Yesterday was ${PREVDATE}"
Clay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-09-2001 12:44 PM
тАО10-09-2001 12:44 PM
Re: date manipulating
Yesterday=`TZ=CST+24 date +%D` works perfect! The only stipulation is, how could I filter for weekends to display Friday's date on Monday?
Thanks!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-09-2001 12:52 PM
тАО10-09-2001 12:52 PM
SolutionSo:
#!/usr/bin/sh
TODAYJDATE=`caljd.sh`
TODAYWKDAY=`caljd.sh -w`
NDAYS=1
#if Monday subtract 3 else subtract 1
if [ ${TODAYWKDAY} -eq 1 ]
then
NDAYS=3
fi
PREVJDATE=$(( ${TODAYJDATE} - ${NDAYS} ))
PREVDATE=`caljd.sh ${PREVJDATE}`
echo "Yesterday was ${PREVDATE}"
Clay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-09-2001 12:57 PM
тАО10-09-2001 12:57 PM
Re: date manipulating
The trick posted by Ramesh only works +-24 hours, so that you can get tomorrorw's or yesterday's date but nothing beyond those bounds.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-09-2001 01:13 PM
тАО10-09-2001 01:13 PM
Re: date manipulating
As pointed out by James and as you have already found out, my suggestion only works for +/- 24 hours, the best way to achieve what you want is to use the caljd.sh script provided by clay.
You could do something like this
/Begin/
#!/usr/bin/ksh
# THis script calculates the previous date of weekdays
# Uses caljd.sh
PATH=/usr/bin:/usr/local/bin:
# You do want to modify the path to include the caljd.sh script
TODAYJDATE=`caljd.sh`
TODAY_WK_DAY=`caljd.sh -w`
if [ $TODAY_WK_DAY -ge 2 -a $TODAY_WK_DAY -le 5 ]
then
{
PREVJDATE=$(( ${TODAYJDATE} - 1))
PREVDATE=`caljd.sh ${PREVJDATE}`
echo "Yesterday was ${PREVDATE}"
}
elif [ $TODAY_WK_DAY -eq 1 ]
then
{
PREVJDATE=$(( ${TODAYJDATE} - 3))
PREVDATE=`caljd.sh ${PREVJDATE}`
echo "Yesterday was ${PREVDATE}"
}
elif [ $TODAY_WK_DAY -eq 0 ]
then
{
PREVJDATE=$(( ${TODAYJDATE} - 2))
PREVDATE=`caljd.sh ${PREVJDATE}`
echo "Yesterday was ${PREVDATE}"
}
elif [ $TODAY_WK_DAY = 6 ]
then
{
PREVJDATE=$(( ${TODAYJDATE} - 1))
PREVDATE=`caljd.sh ${PREVJDATE}`
echo "Yesterday was ${PREVDATE}"
}
fi
/End/
This would work for all the scenarious
-HTH
Ramesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-09-2001 01:16 PM
тАО10-09-2001 01:16 PM
Re: date manipulating
Modified version,
/Begin/
#!/usr/bin/ksh
# THis script calculates the date depending on weekdays
# Uses caljd.sh
PATH=/usr/bin:/usr/local/bin:~/admin/scripts
TODAYJDATE=`caljd.sh`
# TODAY_WK_DAY=`caljd.sh -w`
TODAY_WK_DAY=6
if [ $TODAY_WK_DAY -ge 2 -a $TODAY_WK_DAY -le 6 ]
then
{
PREVJDATE=$(( ${TODAYJDATE} - 1))
PREVDATE=`caljd.sh ${PREVJDATE}`
echo "Yesterday was ${PREVDATE}"
}
elif [ $TODAY_WK_DAY -eq 1 ]
then
{
PREVJDATE=$(( ${TODAYJDATE} - 3))
PREVDATE=`caljd.sh ${PREVJDATE}`
echo "Yesterday was ${PREVDATE}"
}
elif [ $TODAY_WK_DAY -eq 0 ]
then
{
PREVJDATE=$(( ${TODAYJDATE} - 2))
PREVDATE=`caljd.sh ${PREVJDATE}`
echo "Yesterday was ${PREVDATE}"
}
fi
/End/
-Regards
Ramesh