Comparing current date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing current date
# 1  
Old 11-03-2010
Comparing current date

Hi,

I have start date and end date in the following format. I need to check the current date is greater than the start date and less than the end date. if i use the command date --date "Tue 6:00 AM", it takes next Tues day not the current week's Tues day. Is there a way to get the current Tues day?
start date-end date
Tue 6:00 AM-Wed 10:00 PM

Thanks,
Selva
# 2  
Old 11-03-2010
Quote:
Originally Posted by bharathappriyan
Hi,

I have start date and end date in the following format. I need to check the current date is greater than the start date and less than the end date. if i use the command date --date "Tue 6:00 AM", it takes next Tues day not the current week's Tues day. Is there a way to get the current Tues day?
start date-end date
Tue 6:00 AM-Wed 10:00 PM

Thanks,
Selva
Try:
Code:
date --date 'Tue 6:00 AM 1 week ago'

BTW: Works only with the GNU version of date.
# 3  
Old 11-03-2010
If i run the process on Tuesday, it will take previous week date instead of current Tues day. I need to get current week date.

Thanks.
# 4  
Old 11-03-2010
Is this not what you want?
Code:
$ date --date 'Tue 6:00 AM'
Tue Nov  9 06:00:00 CET 2010
$
$ date --date 'Tue 6:00 AM 1 week ago'
Tue Nov  2 06:00:00 CET 2010
$

# 5  
Old 11-03-2010
No. I need to check the current date is between the start date or not. If i run the command with 1 week ago, the start date will be Tue Nov 2 06:00:00 EDT 2010
but End date will be Wed Oct 27 22:00:00 EDT 2010 which wrong. I hope that i am making you clear.

Thanks,
bharathappriyan
# 6  
Old 11-03-2010
Play around with this example:
Code:
#!/bin/ksh

# Works only with GNU date

d1="Tue Nov 02 22:00:00 EDT 2010"
d2="Fri Nov 05 22:00:00 EDT 2010"
cdate=$(date)

dat1=$(date +%s -d "$d1")
dat2=$(date +%s -d "$d2")
curr=$(date +%s -d "$cdate")

if [ $curr -gt $dat1 -a $curr -lt $dat2 ]
then
  echo Current date is between "$d1" and "$d2"
fi

# 7  
Old 11-03-2010
How about replace Tue (current day) with blank giving "6:00am":

Code:
START="Tue 6:00 AM"
END="Wed 10:00 PM"
DOW=$(date +%a)
START=${START/$DOW/}
END=${$END/$DOW/}
NOW=$(date +%s)
if [ $NOW -ge $(date --date $START +%s) -a $NOW -le $(date --date $END +%s) ]
then
   # Your code here
fi

I know if today is Thursday the compare is with next weeks dates, but that's fine as result (not within range) is still what you want.

--- Edit----
But my solution still dosn't work for Mon 8:00AM-Thu 9:00PM, problem is you can't apply the DOW modifiers to any date except the current date.
What we need is date --date "Monday 9:00AM from 6 days ago"

Last edited by Chubler_XL; 11-03-2010 at 04:48 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to replace a parameter(variable) date value inside a text files daily with current date?

Hello All, we what we call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to... (1 Reply)
Discussion started by: pradeepp
1 Replies

2. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

3. UNIX for Beginners Questions & Answers

UNIX script to replace old date with current date dynamically in multiple files present in a folder

I am trying to work on a script where it is a *(star) delimited file has a multiple lines starts with RTG and 3rd column=TD8 I want to substring the date part and I want to replace with currentdate minus 15 days. Here is an example. iam using AIX server $ cat temp.txt RTG*888*TD8*20180201~... (1 Reply)
Discussion started by: Shankar455
1 Replies

4. Shell Programming and Scripting

Comparing the dates with the current date in perl scripting

Hi i have a file containg dates likebelow 4/30/2013 3/31/2013 4/30/2013 4/16/2013 4/30/2013 4/30/2013 5/30/2013 5/30/2013 4/30/2013 5/30/2013 5/30/2013 3/31/2013 now i want to compare the above dates with current date and i want to display the difference . (10 Replies)
Discussion started by: siva kumar
10 Replies

5. UNIX for Dummies Questions & Answers

Comparing Output Date to Current System Date

Hi Guys, Anyone who knows how to compare the current date with the a file containing a date, say for example I have a file that looks like this: Command was launched from partition 0. ------------------------------------------------ Executing command in server server6 Fri Dec 16... (7 Replies)
Discussion started by: rymnd_12345
7 Replies

6. UNIX for Dummies Questions & Answers

Comparing two files with datestamp to current date

Hi, I am new to unix and I am stuck on how to compare two .zip file with date stamp in my directory. I need to compare out of the two file which is oldest to current date and unzip it after that done continue to unzip the second zip file. Thanks for your help. (5 Replies)
Discussion started by: lilvi3tboix1
5 Replies

7. UNIX for Dummies Questions & Answers

Delete a row from a file if one column containing a date is greater than the current system date

Hello gurus, I am hoping someone can help me with the required code/script to make this work. I have the following file with records starting at line 4: NETW~US60~000000000013220694~002~~IT~USD~2.24~20110201~99991231~01~01~20101104~... (4 Replies)
Discussion started by: chumsky
4 Replies

8. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

9. UNIX for Dummies Questions & Answers

comparing field to current year

Hi, I have a comma delimited file that contains name, account number, and account date/time(example record below). I want to pull off all the records that have an account date greater than 8/1 of the current year, and create a new file with those records. So for this year, it would take... (6 Replies)
Discussion started by: keeferb
6 Replies

10. Shell Programming and Scripting

Perl: Extracting date from file name and comparing with current date

I need to extract the date part from the file name (20080221 in this ex) and compare it with the current date and delete it, if it is a past date. $file = exp_ABCD4_T-2584780_upto_20080221.dmp.Z really appreciate any help. thanks mkneni (4 Replies)
Discussion started by: MKNENI
4 Replies
Login or Register to Ask a Question