Date operations in Unix?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date operations in Unix?
# 1  
Old 11-25-2009
Date operations in Unix?

Hi Friends,

I need help in below requirements,
1. I have to get current datetime + <mins> into a variable
2. I have to compage dates like,
A=01-JAN-2009 10:20:10
B=01-JAN-2009 10:30:00

C=<same format date as above>
I have to find whether,
1. C is less than A OR,
2. C is greater than B OR,
3. C is in between A and B.

Please help me in this.

Thanks,
Raja.
# 2  
Old 11-25-2009
TRy Perderabo's datecalc script:
https://www.unix.com/answers-frequent...rithmetic.html

Convert A to epoch seconds (%s), then B to epoch seconds (%s), C to epoch seconds.
You now have three numbers to compare to answer your questions.
# 3  
Old 11-25-2009
Try this: In my example below, it is assuming B is always greater than A. This could be coded to where it doesn't matter as well.

Code:
#!/bin/ksh

DATE_A=$(date +%Y%m%d%H%M%S -d "$1")
DATE_B=$(date +%Y%m%d%H%M%S -d "$2")

DATE_C=$(date +%Y%m%d%H%M%S)

if [ "${DATE_C}" -gt "${DATE_B}" ]; then
   echo "C, ${DATE_C},  is greater than B, ${DATE_B}"
elif [ "${DATE_C}" -gt "${DATE_A}" ]; then
   echo "DATE_C, ${DATE_C},  is between A, ${DATE_A}, and B, ${DATE_B}"
else
   echo "C, ${DATE_C},  is less than A, ${DATE_A}"
fi


tests
Code:
$./chk_date.ksh "01-JAN-2009 10:20:10" "12-DEC-2009 10:30:00"
C, 20091125051733,  is between A, 20090101102010, and B, 20091212103000

$ ./chk_date.ksh "01-DEC-2009 10:20:10" "12-DEC-2009 10:30:00"
C, 20091125051752,  is less than A, 20091201102010

$ ./chk_date.ksh "01-JAN-2009 10:20:10" "01-JAN-2009 10:30:00"
C, 20091125051824,  is greater than B, 20090101103000

# 4  
Old 11-25-2009
Quote:
Originally Posted by jsmithstl
Try this: In my example below, it is assuming B is always greater than A. This could be coded to where it doesn't matter as well.

Code:
#!/bin/ksh

DATE_A=$(date +%Y%m%d%H%M%S -d "$1")
DATE_B=$(date +%Y%m%d%H%M%S -d "$2")

DATE_C=$(date +%Y%m%d%H%M%S)

if [ "${DATE_C}" -gt "${DATE_B}" ]; then
   echo "C, ${DATE_C},  is greater than B, ${DATE_B}"
elif [ "${DATE_C}" -gt "${DATE_A}" ]; then
   echo "DATE_C, ${DATE_C},  is between A, ${DATE_A}, and B, ${DATE_B}"
else
   echo "C, ${DATE_C},  is less than A, ${DATE_A}"
fi


tests
Code:
$./chk_date.ksh "01-JAN-2009 10:20:10" "12-DEC-2009 10:30:00"
C, 20091125051733,  is between A, 20090101102010, and B, 20091212103000

$ ./chk_date.ksh "01-DEC-2009 10:20:10" "12-DEC-2009 10:30:00"
C, 20091125051752,  is less than A, 20091201102010

$ ./chk_date.ksh "01-JAN-2009 10:20:10" "01-JAN-2009 10:30:00"
C, 20091125051824,  is greater than B, 20090101103000

This is working fine..
Thank you friend!

For date B, I need to add some mins with current date. How could i get it?



---------- Post updated at 10:36 PM ---------- Previous update was at 10:34 PM ----------

Quote:
Originally Posted by jim mcnamara
TRy Perderabo's datecalc script:
https://www.unix.com/answers-frequent...rithmetic.html

Convert A to epoch seconds (%s), then B to epoch seconds (%s), C to epoch seconds.
You now have three numbers to compare to answer your questions.
I have seen epoch seconds method in net, It is using perl command.
I do not have perl in my system. Thanks for your quick response friend Smilie
# 5  
Old 11-27-2009
Can you explain what you mean by needing to add some minutes with current date?
# 6  
Old 11-30-2009
Quote:
Originally Posted by jsmithstl
Can you explain what you mean by needing to add some minutes with current date?
I have to do comparison between two date values. One is current date time (min) and another date (max) will be calculated by adding some input minutes with current date time.
# 7  
Old 12-02-2009
Quote:
Originally Posted by smr_rashmy
I have to do comparison between two date values. One is current date time (min) and another date (max) will be calculated by adding some input minutes with current date time.
Pls help to get the above requirement.
Ex:
Min_Date=Current Datetime (Format: DD-MON-YYYY HH24:MI:SS)
Max_Date=Current Datetime + X mins (Format: DD-MON-YYYY HH24:MI:SS)
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To skip operations in UNIX shell

hi i am having a acript for which i need to skip the execution of some lines and to continue with remaining lines for eg script.sh rm text for i in * do . . . . . if then rm i want to skip the execution of the lines and to start with (11 Replies)
Discussion started by: rohit_shinez
11 Replies

2. Shell Programming and Scripting

How to search and append words in the same file using unix scripting file operations

Hi , I have a file myhost.txt which contains below, 127.0.0.1 localhost 1.17.1.5 atrpx958 11.17.10.11 atrpx958zone nsybhost I need to append words only after "atrpx958" like 'myhost' and 'libhost' and not after atrpx958zone. How to search the word atrpx958(which is hostname) only,... (5 Replies)
Discussion started by: gsreeni
5 Replies

3. IP Networking

What is the best Unix-like for firewalling operations

Hello from France, I'd like to have your opinion on this : What unix-like would you choose for high bandwidth netwoking operations like a cluster of statefull firewalls ? NetBSD, Linux, others ? Thank you. Best regards. Vincent. (0 Replies)
Discussion started by: vrzs
0 Replies

4. Shell Programming and Scripting

Unix File operations

Hi, Iam having the two files as follows: file1: ASQWEDFR09876543121234512 POIUYTREW09876512345676788 ZXCVBNMKS1209888888888888 file2: ASQWEDFR09876543121234516 asdcvfgbtg@abc.com 0000000-90-1239--2008 8990---- CXADFGTU09876543121234789 asdcvfgbtg@abc.com ... (14 Replies)
Discussion started by: nivas
14 Replies

5. Shell Programming and Scripting

Unix file operations(shell script)

Hi, I want to compare two files. Files will look like as follows: file1: ASDFGHJU|1234567890123456 QWERTYUI|3456789098900890 file2: ZXCVBVNM|0987654321234567 POLKIJUYH|1234789060985478 output file should be: ASDFGHJU|1234567890123456 QWERTYUI|3456789098900890 Thnaks in advance (6 Replies)
Discussion started by: nivas
6 Replies

6. UNIX Desktop Questions & Answers

where i can find list of UNIX commands for daily operations ?

Hi There, Can anyone help, where i can find list of UNIX commands just for regulat day ro day operations Thanx MGR (1 Reply)
Discussion started by: mgoutham
1 Replies

7. UNIX for Dummies Questions & Answers

mathematics operations in unix

Hello guys! Can say me anybody about operatios with unix, I don't to make operations, only inside in a variable, like this #y=4 #x=2 #let z=$y-$x #echo $z # 2 but I can't to make mathematical operations with decimal like this #y=3.2 #x=1.5 #let z=$y-$x #echo $z # 3 this... (2 Replies)
Discussion started by: cesar720213
2 Replies
Login or Register to Ask a Question