Unix has date function which is good to add or subtract days / time from current date but has lots of limitation. I found no easy way to do serious date-time arithmetic using it. One possible way is to convert date-time to seconds (since epoch) and then manipulate it.
#Assign a date to variable. startDateTime="2013-04-28 00:00:00" #Converts date to epoch and assign it to variable. #One should be careful as it may accept it as date in only few predefined formats startDateInSecs=`date -d "$StartDateTime" +"%s"` #Perform manipulation by adding or subtracting time. #For example to add 2 hours let newDateInSecs=$((StartDateInSecs + 2*3600)) #Then print or assign the date to a variable in required format. NewDateTime=`date -d @"$newDateInSecs" +"%Y-%m-%d %H:%M:%S"` echo NewDateTime #Do note @ before variable $newDateInSecs. That shows date-time is in seconds since epoch
No comments:
Post a Comment