Hi,
I want to subtract 'n' days from the current timestamp in a k shell script. Is there any inbuilt function to do it
or any workaround solution to get the date. And I want the output to be in YYYY:MM:DD HH:MM:SS format. Please help.
Thanks in advance.
In ksh88, no there are no builtins to do what you want. You have to resort to perl or a ksh script that turns dates into JD, subtract days, then turn the new JD back into a date.
See cfa johnson's scripts for Julian dates:
8: The Dating Game
If you are using ksh93 you can use %T. For example to print the date 5 days ago:
$ printf "%(%Y:%m:%d %H:%M:%S)T\n" "5 days ago"
2011:02:05 10:20:15
$
May i know, how to get 10 days date from currentdate.
Like current date: 04-11-2011
I need: 03-28-2011.
I am using this but when i use it in my query the result is diffrent
Code: date -d "14 days ago"
Use this script with Number of days as the parameter
datefunction.ksh
#!/bin/bash
sec2date (){
date --utc --date "1970-01-01 $1 sec" "+%Y-%m-%d %T"
}
sec=86400
NDAYS=$1
SEC_DIFF=`expr $sec \* $NDAYS`
CURR_TS=`date "+%Y-%m-%d %T"`
dte1=`date --utc --date "$CURR_TS" +%s`
dte2=$((dte1-SEC_DIFF))
dte2op=$(sec2date $dte2)
echo "Current Timestamp: $CURR_TS"
echo "Timestamp $1 days before: $dte2op"
exit 0
Use like "datefunction.ksh 5"
Here 5 is the number of days