Sponsored Content
Top Forums Shell Programming and Scripting Calculate the number of days between 2 dates - bash script Post 302700617 by rdrtx1 on Thursday 13th of September 2012 06:49:58 PM
Old 09-13-2012
a ksh solution

Code:
 
#/bin/ksh
 
# date format: yyyy-mm-dd
 
DATE1="2002-04-30"
DATE2="2006-12-02"
 
typeset -L4 y1 y2
typeset -Z -R2 mm dd
 
y1=$DATE1
y2=$DATE2
fy=$y1
my=$DATE1
my=${my#*-}
my=${my%-*}
 
c=0
d2=0
while [[ $y1 -le $y2 ]]
do
for mm in 1 2 3 4 5 6 7 8 9 10 11 12 do
[[ $y1 -eq $fy ]] && [[ $mm -lt $my ]] && continue cal $mm $y1 | while read line do
line=${line%%[a-zA-Z]*} [[ -n ${line:-""} ]] && {
for dd in $line do
(( c = c + 1 )) [[ "$y1-$mm-$dd" = "$DATE1" ]] && { d1=$c ; } [[ "$y1-$mm-$dd" = "$DATE2" ]] && { d2=$c ; break ; }
done
} [[ ${d2:-0} -gt 0 ]] && break
done [[ ${d2:-0} -gt 0 ]] && break
done (( y1 = y1 + 1 ))
done (( days = d2 - d1 )) echo $days


Last edited by rdrtx1; 09-14-2012 at 02:21 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find number of days and list out the dates in between

Hi All, Can unix cshell list out the number of days between 070201 and 070205 (format is yymmdd) and list out all the dates in between in similiar format. set startdate = `date '+%y%m%d'` #eg 070201 set enddate = `date '+%y%m%d'` #eg 070205 i would expect the number of days to be 5... (2 Replies)
Discussion started by: Raynon
2 Replies

2. UNIX for Advanced & Expert Users

Number of days between two distinct dates

Hi I'm looking for a .ksh script/function that will calculate ONLY the number of days between two distinct dates. Further convert the number of days to weeks and display. I need this to be part of another larger script that checks the password expiry on several servers and notifies the... (1 Reply)
Discussion started by: radheymohan
1 Replies

3. Shell Programming and Scripting

calculate the number of days left in a month

does any one have any ideas how i would go about calculating the number of days left in the month from a bash script ?. I want to do some operations on a csv file according to the result (8 Replies)
Discussion started by: dunryc
8 Replies

4. Shell Programming and Scripting

Script to calculate user's last login to check if > 90 days

I need a script to figure out if a user's last login was 90 days or older. OS=AIX 5.3, shell=Korn Here's what I have so far: ==== #!/usr/bin/ksh NOW=`lsuser -a time_last_login root | awk -F= '{ print $2 }'` (( LAST_LOGIN_TIME = 0 )) (( DIFF = $NOW - $LAST_LOGIN_TIME )) lsuser -a... (3 Replies)
Discussion started by: pdtak
3 Replies

5. Shell Programming and Scripting

Calculate 30/31 days from today date script

Hi Guys, I was working some time ago n was in need to calculate date 30/31 days from today including Feb (Leap yr stuff). Today date is variable depending on day of execution of script. I tried searching but was not able to get exactly what I needed....So at that I time I implemented by my own... (3 Replies)
Discussion started by: coolgoose85
3 Replies

6. Shell Programming and Scripting

Get number of days between given dates

Hi I need one single command to get number of days between two given dates.datecalc is not working. ex. fromdate:01.04.2010 todate :24.04.2010 i should get the out put as 23 Thanks in advance (4 Replies)
Discussion started by: suryanarayana
4 Replies

7. Shell Programming and Scripting

Calculate days between yyyyMmmdd dates on Solaris

I extract dates from the log file and need to calculate days between two dates. My dates are in yyyyMmmdd format. Example: $d1=2011 Oct 21 $d2=2012 Feb 20 I need to calculate the number of days between $d2 and $d1. This is on Solaris. Any ideas? Thanks, djanu (4 Replies)
Discussion started by: djanu
4 Replies

8. Web Development

Calculate the number of days between 2 dates - PHP

Is this code good for this purpose? <?php $date1 = mktime(0,0,0,01,01,1991); $date2 = mktime(0,0,0,03,22,2012); $diff = $date2 - $date1; $days = $diff / (60*60*24); echo ($days . "<br />"); ?> (3 Replies)
Discussion started by: kovacsakos
3 Replies

9. Shell Programming and Scripting

Shell script to calculate difference between 2 dates

shell script to calculate difference between 2 dates (3 Replies)
Discussion started by: gredpurushottam
3 Replies

10. Shell Programming and Scripting

Get number of days between 2 dates

Gents. Please can u help. I would like to calculate the days between two dates. Example file1 ( previous date) file1 - Input file 9/29/2010 10195 9/29/2010 1057 2/2/2016 10 2/2/2016 10169 2/2/2016 1057 2/3/2016 10005 2/3/2016 10014 In file2 I add the actual date using this code.... (9 Replies)
Discussion started by: jiam912
9 Replies
DumpXML(3pm)						User Contributed Perl Documentation					      DumpXML(3pm)

NAME
Data::DumpXML - Dump arbitrary data structures as XML SYNOPSIS
use Data::DumpXML qw(dump_xml); $xml = dump_xml(@list) DESCRIPTION
This module provides a single function called dump_xml() that takes a list of Perl values as its argument and produces a string as its result. The string returned is an XML document that represents any Perl data structures passed to the function. Reference loops are han- dled correctly. The following data model is used: data : scalar* scalar = undef | str | ref | alias ref : scalar | array | hash | glob | code array: scalar* hash: (key scalar)* The distribution comes with an XML schema and a DTD that more formally describe this structure. As an example of the XML documents produced, the following call: $a = bless [1,2], "Foo"; dump_xml($a); produces: <?xml version="1.0" encoding="US-ASCII"?> <data xmlns="http://www.cpan.org/.../Data-DumpXML.xsd"> <ref> <array class="Foo"> <str>1</str> <str>2</str> </array> </ref> </data> If dump_xml() is called in a void context, then the dump is printed on STDERR automatically. For compatibility with "Data::Dump", there is also an alias for dump_xml() called simply dump(). "Data::DumpXML::Parser" is a class that can restore data structures dumped by dump_xml(). Configuration variables The generated XML is influenced by a set of configuration variables. If you modify them, then it is a good idea to localize the effect. For example: sub my_dump_xml { local $Data::DumpXML::INDENT = ""; local $Data::DumpXML::XML_DECL = 0; local $Data::DumpXML::DTD_LOCATION = ""; local $Data::DumpXML::NS_PREFIX = "dumpxml"; return dump_xml(@_); } The variables are: $Data::DumpXML::INDENT You can set the variable $Data::DumpXML::INDENT to control the amount of indenting. The variable contains the whitespace you want to be used for each level of indenting. The default is a single space. To suppress indenting, set it to "". $Data::DumpXML::INDENT_STYLE This variable controls where end element are placed. If you set this variable to the value "Lisp" then end tags are not prefixed by NL. This give a more compact output. $Data::DumpXML::XML_DECL This boolean variable controls whether an XML declaration should be prefixed to the output. The XML declaration is the <?xml ...?> thingy. The default is 1. Set this value to 0 to suppress the declaration. $Data::DumpXML::NAMESPACE This variable contains the namespace used for the XML elements. The default is to let this be a URI that actually resolve to the XML schema on CPAN. Set it to "" to disable use of namespaces. $Data::DumpXML::NS_PREFIX This variable contains the namespace prefix to use on the elements. The default is "", which means that a default namespace will be declared. $Data::DumpXML::SCHEMA_LOCATION This variable contains the location of the XML schema. If this variable is non-empty, then an "xsi:schemaLocation" attribute is added to the top level "data" element. The default is not to include this, as the location can be inferred from the default XML namespace used. $Data::DumpXML::DTD_LOCATION This variable contains the location of the DTD. If this variable is non-empty, then a <!DOCTYPE ...> is included in the output. The default is to point to the DTD on CPAN. Set it to "" to suppress the <!DOCTYPE ...> line. BUGS
Class names with 8-bit characters are dumped as Latin-1, but converted to UTF-8 when restored by the Data::DumpXML::Parser. The content of globs and subroutines are not dumped. They are restored as the strings "** glob **" and "** code **". LVALUE and IO objects are not dumped at all. They simply disappear from the restored data structure. SEE ALSO
Data::DumpXML::Parser, XML::Parser, XML::Dumper, Data::Dump AUTHORS
The "Data::DumpXML" module is written by Gisle Aas <gisle@aas.no>, based on "Data::Dump". The "Data::Dump" module was written by Gisle Aas, based on "Data::Dumper" by Gurusamy Sarathy <gsar@umich.edu>. Copyright 1998-2003 Gisle Aas. Copyright 1996-1998 Gurusamy Sarathy. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.8.8 2006-04-08 DumpXML(3pm)
All times are GMT -4. The time now is 02:38 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy