I'm late to the party, but I think we have a few problems that need to be sorted out here.
The first is the ambiguity in the request. It appears the OP wants to take a given input date, in ddmmyyyy
format, and use it to get the previous day, output in dd/mm/yyyy
format. Is that correct? I'm going to assume so for the rest of this post.
The second problem is that he initially failed to mention that he wants to do this on a Unix machine. This seriously restricts the options available. While the gnu toolset has a reasonable set of functions for converting arbitrary dates, the ones available in most other Unixes do not. Always be sure to mention the environment you're using if it's something different from the assumed Linux+Gnu tools.
Third, since the input timestamp is in a non-standard and hard-to-use format, it's probably necessary to break it up and re-order it into something easier for other tools to understand. If it's possible, I recommend always using the ISO 8601
standard yyyymmdd format instead. It would save a lot of headaches in the long run.
Now, as mentioned, on a Linux system, this is relatively easy. Here's how I'd handle it:
date -d "$year-$month-$day -1 day" "+%d/%m/%Y"
The first part, which breaks up the string, can be done on any system since it uses posix-standard shell syntax. But the date command won't work, since it relies on the gnu-only -d
, in long form) option.
So on Unix we'd generally have to do something more complex, but what that is depends on the tools available to you. A more powerful language with actual time-conversion functions built in, like perl
, would be the best bet. Otherwise you'd probably have to do something like convert the date string to the epoch time, subtract a days worth of seconds from it, then convert it back.
Unfortunately I'm afraid I can't give you any direct solutions at this point. I'm not that familiar with perl
, nor is it clear to me what is and isn't supported by the other Unix tools. The OP should clarify what tools he has available, and then perhaps someone with more experience with them can help out.