Converting String Date into UNIX Date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting String Date into UNIX Date
# 1  
Old 04-27-2018
Converting String Date into UNIX Date

Hi,
I have a string date to my unix script(sun solaris). I wanted to convert it into unix date so that I can use it in a conditional statement. Please see below:

Code:
 
 MyTest.sh -s 2018-05-09
  
suppdt=$1 # string date passed via arguement as 2018-04-09
 
curryr=`date '+%Y'`
nextyr=`expr $curryr + 1`
 
StartDt_v=`echo "${curryr}-05-01"`
EndDt_v=`echo "${nextyr}-08-15"`
  
 curryrfilename=test-import-data-${curryr}.txt
 nextyrfilename=test-import-data-${nextyr}.txt
  
 if [ ${suppdt} -ge ${StartDt_v} ] && [ ${suppdt} -le ${EndDt_v} ]; then
     rm -f nextyrfilename;
 else
    echo " Do nothing"
 fi

The issue is every time the conditional statement is evaluated it goes into else part of IF statement.

I think this might be happening due to the StartDt_v and EndDt_v being a String date instead of proper Date variable. Would appreciate any advice from experts.

Thanks
# 2  
Old 04-27-2018
You are using integer operators to compare string variables - that won't work. You have two options here:
- use string operators > and < BUT make sure to escape them as the shell might treat them as redirections otherwise.
- make the dates integers using shell expansion like if [ ${suppdt//-} -ge ${StartDt_v//-} ] && . . .
# 3  
Old 04-27-2018
try:

Code:
if [[ $(expr "${suppdt}" \>= "${StartDt_v}") -eq 1 && $(expr "${suppdt}" \<= "${EndDt_v}") -eq 1 ]]

or
Code:
if [[ "${suppdt}" > "${StartDt_v}" || "${suppdt}" = "${StartDt_v}" ]] && [[ "${suppdt}" < "${EndDt_v}" || "${suppdt}" = "${EndDt_v}" ]]


Last edited by rdrtx1; 04-27-2018 at 04:59 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare Date with String in date format

Hi, I am new to this forum, searched, but for some reason did not find much help, so a new thread. I am trying to compare date with a string which is in date format , but for some reason, system does not give me the right result. Date is coming in a file and i am comparing the same with... (2 Replies)
Discussion started by: mkstool
2 Replies

2. UNIX for Dummies Questions & Answers

Converting string date time to unix time in AWK

I'd like to convert a date string in the form of sun aug 19 09:03:10 EDT 2012, to unixtime timestamp using awk. I tried This is how each line of the file looks like, different date and time in this format Sun Aug 19 08:33:45 EDT 2012, user1(108.6.217.236) all: test on the 17th ... (2 Replies)
Discussion started by: bkkid
2 Replies

3. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

4. Shell Programming and Scripting

Converting date string to different formats

Sucks to be a noob :o See my last post for solution I have 3 different log formats and the filenames contain a date. I am trying to write a script to grep these files between two date ranges. I am now stuck at the date conversion bit. I let the user entered a date string in the format... (6 Replies)
Discussion started by: GermanJulian
6 Replies

5. Homework & Coursework Questions

Date comparison with 'string date having slashes and time zone' in Bash only

1. The problem statement, all variables and given/known data: I have standard web server log file. It contains different columns (like IP address, request result code, request type etc) including a date column with the format . I have developed a log analysis command line utility that displays... (1 Reply)
Discussion started by: TariqYousaf
1 Replies

6. Shell Programming and Scripting

Converting string to date in perl

Hi, I need convert a date string to date. For eaxmple $last_date=6/2/2009 and I want to change the format of the above mentioned date to "Jun 2 2009 12:00AM". Do we have any functionality or staright method to convert to the desired format? (4 Replies)
Discussion started by: siba.s.nayak
4 Replies

7. Shell Programming and Scripting

Convert String to Date Unix

Hi people, I need to convert a string eg 09/13/2008 to a valid unix date. (4 Replies)
Discussion started by: sameerspice
4 Replies

8. HP-UX

a simple way of converting a date in seconds to normal date

Hi all! I'm working on a HPUX system, and I was wondering if there is a simple way to convert a date from seconds (since 1970) to a normal date. Thanks (2 Replies)
Discussion started by: travian
2 Replies

9. Shell Programming and Scripting

converting unix date variable to gmt

Hi, I have a ksh script which extracts files from a directory in the following format, eg: 10288.Job.rescheduled.1154647335 I need to extract the unix timestamp (eg 1154647335) and convert it to dd-mm-yyyy. Can anyone suggest anything for this? thanks. :confused: (6 Replies)
Discussion started by: chilli
6 Replies
Login or Register to Ask a Question