[Solved] Question for Perderabo on date calc


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Question for Perderabo on date calc
# 1  
Old 05-16-2013
[Solved] Question for Perderabo on date calc

Hi,
First of all, thanks for all the awesome suggestions on this forum. This helps all the UNIX enthusiast like me.

Now, I had a similar requirement as mentioned in a very old post here:

Question about Perderabo's "Days Elapsed Between Two Dates"

But I am struggling what to change in the following to give me the month as MM always. At the moment it is truncating the 0 from single digit months.


Quote:
./datecalc -a 2003 11 12 - 1 | read y m d ; echo ${y}${m}${d}
E.g.
20130425 instead of 2013425

Thanks!

Last edited by Scott; 05-16-2013 at 06:29 PM.. Reason: Linked URL
# 2  
Old 05-16-2013
Instead of modifying Perderabo's script, you can use printf formatting option to zero lpad:
Code:
$ ./datecalc -a 2003 10 01 - 1 | read y m d
$ printf "%d%02d%02d\n" $y $m $d
20030930

# 3  
Old 05-16-2013
Quote:
Originally Posted by Yoda
Instead of modifying Perderabo's script, you can use printf formatting option to zero lpad:
Code:
$ ./datecalc -a 2003 10 01 - 1 | read y m d
$ printf "%d%02d%02d\n" $y $m $d
20030930


Yoda - Thanks for your prompt reply. But I am not sure if I understand correctly.

Can I use the printf command as an conjuction to Perderabo's script's output?

Basically, the reason why I am using Perderabo's script is to find out the date which is 21 days older than system date in the format of YYYYMMDD. I would really appreciate if there is an easier way to do so without using the Perderabo's script.

P.S. I am using KSH on AIX 6.1.
# 4  
Old 05-16-2013
OK, modify line number 188 to:
Code:
177          ((standard_jd=jd+2400001))
178          ((temp1 = standard_jd + 68569))
179          ((temp2 = 4*temp1/146097))
180          ((temp1 = temp1 - (146097 * temp2 + 3) / 4))
181          ((year  = 4000 * (temp1 + 1) / 1461001))
182          ((temp1 = temp1 - 1461 * year/4 + 31))
183          ((month = 80 * temp1 / 2447))
184          ((day   = temp1 - 2447 * month / 80))
185          ((temp1 = month / 11))
186          ((month = month + 2 - 12 * temp1))
187          ((year  = 100 * (temp2 - 49) + year + temp1))
188          printf "%d%02d%02d\n" $year $month $day
189          return 0

Here is the output after modification:
Code:
./datecalc -a 2003 10 01 - 1
20030930

# 5  
Old 05-16-2013
You could always get coreutils (provides the linux date command and many other things) from the IBM toolbox for linux applications.

I have a heap of scripts on AIX 5.3 that do date manipulation using this technique.
# 6  
Old 05-17-2013
Yoda - Thank you very much for your help. This works just perfectly fine! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[Solved] Using date command, getting previous day

Legends, i need to get previous day using date command. Can you please help. sdosanjh:/home> date +%m%d%y 011514 i tried -d '-1 day' but it is not working (5 Replies)
Discussion started by: sdosanjh
5 Replies

2. Shell Programming and Scripting

Solved: Date: Convert m to mm

Hi all, I've searched through the forum and can't find a thread specific to this question: I have a CSV file with a date column, the dates I'm given from the original file look like: "m/dd/yyyy" (UNLESS it's a double digit month, like October, where it would then show up as "mm/dd/yyyy"). Is... (8 Replies)
Discussion started by: mtucker6784
8 Replies

3. HP-UX

[Solved] Need help in date display

Hi, I have one hp-ux sytem date display problem when viewed remotely hostA 21: date Fri Jul 5 11:18:51 SST 2013 hostA 22: remsh hostB -l username -n date 2013年07月05日 11時32分27秒 But on actual system it display correctly:confused: hostB 21: date Fri Jul 5 11:40:33 SST 2013 (4 Replies)
Discussion started by: jorendain
4 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Previous Year Date

Hi Gurus, I would like to get the date for the previous year based on the date I specify. For e.g. If I input today's date (i.e. 20130422) I need to get 20120422. We don't have GNU and use K Shell. Any help is highly appreciated. Thanks Shash (4 Replies)
Discussion started by: shash
4 Replies

5. Shell Programming and Scripting

[Solved] Date as Input

Below is my Scenario. I wrote one script to search the specific log files for Yesterdays date and gives the result. I need a script like 1. Once I run the Script, The script should ask me which date I want to search. 2. Once i enter the date, That script should search the log files for... (3 Replies)
Discussion started by: Padmanabhan
3 Replies

6. Shell Programming and Scripting

[Solved] Replace yesterday date with today's date except from the first line

Hello, I have a file like this: 2012112920121130 12345620121130msABowwiqiq 34477420121129amABamauee e7748420121130ehABeheheei in case the content of the file has the date of yesterday within the lines containing pattern AB this should be replaced by the current date. But if I use... (3 Replies)
Discussion started by: Lilu_CK
3 Replies

7. Shell Programming and Scripting

[Solved] missing date in unix

i have a file with below contents Mg_Message_count,1-Aug-12,46 Mg_Message_count,2-Aug-12,48 Mg_Message_count,3-Aug-12,48 Mg_Message_count,4-Aug-12,48 Mg_Message_count,5-Aug-12,48 Mg_Message_count,6-Aug-12,48 Mg_Message_count,7-Aug-12,42 Mg_Message_count,20-Aug-12,24... (10 Replies)
Discussion started by: rabindratech
10 Replies

8. Shell Programming and Scripting

date calc

Hi, I need subtract two date values (which are in day of the year format) and the output would give the remaining days. using the command date +"%j" i would get today's 'day of the year' i.e., > date +"%j" 256 Next, i need to take input of a previous date in the format 09/05/2012 and then... (4 Replies)
Discussion started by: sam_bd
4 Replies

9. UNIX for Dummies Questions & Answers

Question about perderabo's MIMETOOL script

Perderabo, I copied the mimetool script and am running it on an HP-UX release 11i, and in most cases it works like a charm. But I ran into the following issue; I extracted data from an Oracle database and formatted the results as a *.csv file so that the user can view it as an excel... (4 Replies)
Discussion started by: perssonc
4 Replies

10. UNIX for Dummies Questions & Answers

Question to a Perderabo script

Hi I tried this date script https://www.unix.com/showthread.php?p=16559#post16559 but I am probably doing something wrong. If i type: datecalc -a 2006 05 01 + 5 I get following error message: datecalc: Syntax error at line 207 If i look at line 207 i dont get it! But thats not surprising. ... (2 Replies)
Discussion started by: swissman24
2 Replies
Login or Register to Ask a Question