Help on Dates in Shell Scripting


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

Hi Experts,

I am using the below code to get the previous day based on the date given as a input.

Code:
#!/usr/bin/ksh

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

Output: 20130714

How can i display the same output in 14/07/2013, i tired '+%d/%m/%y'` but i am getting error.

also how can i get the first day of the month based on the date which is fetched $yest.

Any help.

Thanks

Last edited by Scott; 07-16-2013 at 01:15 AM.. Reason: Code tags
# 2  
Old 07-16-2013
Do you have GNU date? just check man page of date if u have GNU date then you have option to get yesterday's date..

If not you have to modify TZ to get the desired date.. date - 1 wont work Smilie
# 3  
Old 07-16-2013
If your date command supports the -d option (gnu date)
you can do:

Yesterday:
Code:
date -d "-1 day" +%d/%m/%Y


1st of month:
Code:
YM=`date +%Y%m`
date -d ${YM}01 +%d/%m/%Y

# 4  
Old 07-16-2013
Hi Experts,

when i run the command in unix prompt: i got the below error:
Code:
date -d "-1 day" +%d/%m/%Y
date: illegal option -- d
Usage: date [-u] [+format]
       date [-u] [mmddhhmm[[cc]yy]]
       date [-a [-]sss.fff]

can you help me to sort this.

regards,

Last edited by Scott; 07-16-2013 at 01:15 AM.. Reason: Please use code tags
# 5  
Old 07-16-2013
OK you don't have gnu date you might have to use perl

yesterday.pl
Code:
#!/bin/perl
use POSIX; print strftime('%d/%m/%Y', localtime(time() - 24*60*60));

first.pl
Code:
#!/bin/perl
use POSIX;
my $year, $month;
($year, $month) = split(/\s/, strftime('%Y %m', localtime(time())));
printf strftime('%d/%m/%Y', localtime(mktime(0,0,0,1,$month-1,$year-1900,0,0)));


Last edited by Chubler_XL; 07-16-2013 at 01:39 AM..
# 6  
Old 07-16-2013
Hi Experts,

I want to use in unix script.

any help/suggestions

regards,
# 7  
Old 07-16-2013
For your first requirement, this should work
Code:
$ date -j -f "%s" $(($(date "+%s") - 86400))  "+%d/%m/%Y"
15/07/2013

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