Help on Dates in Shell Scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help on Dates in Shell Scripting
# 8  
Old 07-16-2013
Hi Experts,

GNU date is not supporting in my UNIX prompt, so based on today's date how can i get the first day' of the month. i want the first date of the month.

for example:

if current date is 16/07/2013, i want the 01/07/2013. i mean first of the month.

Thanks,

Last edited by learner24; 07-16-2013 at 02:47 AM.. Reason: Spell check
# 9  
Old 07-16-2013
I am not sure if this is what you need..just check out..

Code:
$ date "+%d/%m/%Y"  | awk -F"/" '{print "01"FS$2FS$3}' 
01/07/2013

# 10  
Old 07-16-2013
Hi Experts,

Thanks, this is working for current month, if we pass a different date like 12/06/2013, will this work?

how can we modify the code to take any date and get the first day of that month.

for example:

if we have input as 12/06/2013,the first date of the month is 01/06/2013.

how can we do this?

thanks in davance.

regards,
# 11  
Old 07-16-2013
Code:
$ echo "12/06/2013" | awk -F"/" '{print "01"FS$2FS$3}' 
01/06/2013
$ echo "31/12/2013" | awk -F"/" '{print "01"FS$2FS$3}' 
01/12/2013

# 12  
Old 07-16-2013
Hi Experts,

can i store this value in variable like.

Code:
inputdate=12/06/2013

Code:
date= echo "inputdate" | awk -F"/" '{print "01"FS$2FS$3}'

echo $date

output: Expected
01/06/2013

regards,

Last edited by Scott; 07-16-2013 at 03:08 AM.. Reason: Code tags, please...
# 13  
Old 07-16-2013
Yes. I would suggest you to give an attempt and try something yourself before posting.

Code:
date=$(echo $inputdate | awk -F"/" '{print "01"FS$2FS$3}')

# 14  
Old 07-16-2013
Hi Experts,

I tired this.

Code:
#!/bin/ksh

inputdate=15/07/2013

year=`expr substr $inputdate 7 4`
month=`expr substr $inputdate 4 2`

FROM_DATE=01/$month/$year
echo $month
echo $year
echo $FROM_DATE

let me know if correct?

---------- Post updated at 02:01 AM ---------- Previous update was at 01:26 AM ----------

Hi Experts,

i am using the below code get the date of previous day.

Code:
#!/usr/bin/ksh

datestamp=`date '+%Y%m%d'`
yest=$((datestamp -1))
echo $yest

When i execute the code i am getting output as:

20130715

What i am trying here is, based on the date passed i am fetching previus day's date.

for example:

If date passed is date = 16/07/2013, i need date of previous day

15/07/2013 but now i am getting output as 20130715, how can i convert this to format 15/07/2013

Thanks in advance.

Last edited by Scott; 07-16-2013 at 04:09 AM.. Reason: Code tags, PLEASE
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing dates in shell script

Hi All, I have a date variable say dt="2014-01-06 07:18:38" Now i need to use this variable to search a log and get the entries which occured after that time. (1 Reply)
Discussion started by: Girish19
1 Replies

2. Shell Programming and Scripting

Shell script to work on dates

Hi Sir/Madam I have a file data.txt like below file_name date_of_creation x 2/10/2012 y 8/11/2010 z 11/3/2013 a 2/10/2013 b 3/10/2013 c ... (4 Replies)
Discussion started by: kumar85shiv
4 Replies

3. 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

4. Shell Programming and Scripting

compare dates using shell sript

I have 2 date feilds 2011-05-13:18:45 2011-05-13:18:30 I need to compare them and say its OK/NOK I tried this but dint work. systime=2011-05-13:18:45 shubtime=2011-05-13:18:30 if then echo" OK" else echo "NOK" fi In this its not same so the o/p should be NOK (2 Replies)
Discussion started by: LavanyaP
2 Replies

5. Shell Programming and Scripting

Difference of 2 dates in shell script

Hi., After retrieving values from DB I have two datestamps in format: 12/01/2010:05:40:00 AM and 12/01/2010:06:00:00 PM. general time format: MM/DD/YYYY:HH:MM:SS AM or PM Any quick solution to get the difference of two in the format : 1 day(s) 12:20:00 Thanks., (6 Replies)
Discussion started by: IND123
6 Replies

6. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

7. Shell Programming and Scripting

Compare two dates using Shell Programming

Hi all, a=2007-05-10 (YYYY-DD-MM Format) b=2007-06-10 These are the two given dates and I need to compare. (First It should split the dates into YYYY,dd,mm) The script should first compare years(2007 here).If both are same or if "a" is lesser than "b"(ie.suppose year in "a" is 2006),it... (4 Replies)
Discussion started by: dave_nithis
4 Replies

8. Shell Programming and Scripting

How to compare the dates in shell script

Hi How to compare created or modified date of two files help needed thanks Vajiramani :) (9 Replies)
Discussion started by: vaji
9 Replies

9. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies

10. Shell Programming and Scripting

comparing 2 dates in Bourne shell

HI, I am able to find days difference using FIND command. However it is comparing between today and the last time it was modified. I now need to find the difference between a date specified by myself and the last time the file was modified. Is there a command which I can use or I have to... (1 Reply)
Discussion started by: scmay
1 Replies
Login or Register to Ask a Question