473,471 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Date Difference between two dates

4 New Member
Hi,

I need to find out the difference between two dates and both the dates are provided by the user.
Please can someone help me on this..Need it urgently !!!

Ex:
Date1 : 2007/12/26
Date2: 2007/12/24
Dec 26 '07 #1
9 8010
numberwhun
3,509 Recognized Expert Moderator Specialist
Hi,

I need to find out the difference between two dates and both the dates are provided by the user.
Please can someone help me on this..Need it urgently !!!

Ex:
Date1 : 2007/12/26
Date2: 2007/12/24
Considering this is a learning forum, you won't be provided the code to do this. Instead, can you post what you have tried thus far and we will try and help you get your code working?

There are a few ways that you can go about this, including converting the dates to epoch time (which is Unix time, time in seconds since January 1st, 1970) and comparing them that way.

Regards,

Jeff
Dec 26 '07 #2
rajiv07
141 New Member
You can use Date::Calc module to find the difference between two dates.

Here is the Link Date::Calc .

Regards
Rajiv
Dec 26 '07 #3
Vinod11
4 New Member
Thanks all...!!
I understand that this is a learning forum and I am not looking out for an exact code for doing this.There is a code which I am using which converts two dates in EPOCH seconds ..calculates the difference of bothe the dates i..e todays date wh0ch it gets from LOCALTIME() and a date which is provided in the script.
However,this does not suffice my requirement as I want the two dates to be provided from command line and then the code should convert the two dates in EPOCH seconds and then do the processing..
And I want some help to accept bothe the dates and convert it into EPOCH seconds..Aftrewhich I will be able to do the rest of processing...
Dec 27 '07 #4
eWish
971 Recognized Expert Contributor
Please post you code that you have tried and indicate where you are having you problems.

--Kevin
Dec 27 '07 #5
Vinod11
4 New Member
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. $date1 = 361535725;    # in seconds
  3. $date2 = 96201950;     # in seconds
  4. $difference = $date1 - $date2;
  5. print "There were $difference seconds between date1 and date2 \n";
  6. $seconds    =  $difference % 60;
  7. $difference = ($difference - $seconds) / 60;
  8. $minutes    =  $difference % 60;
  9. $difference = ($difference - $minutes) / 60;
  10. $hours      =  $difference % 24;
  11. $difference = ($difference - $hours)   / 24;
  12. $days       =  $difference % 7;
  13. $weeks      = ($difference - $days)    /  7;
  14. print "There are $weeks weeks $days days $hours hours $minutes minutes $seconds seconds\n";
  15.  
This is the raw code that I am using.
Here there are two variables date1 and date2 which are in seconds.
My requirement is :
pass dates through command line
convert those dates in epoch seconds ,
from where the script will do its processing..
Dec 27 '07 #6
KevinADC
4,059 Recognized Expert Specialist
pass dates as arguments:

perl yourscript.pl date1 date2

get dates:

Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my ($date1,$date2) = @ARGV;
  5.  
use the Time::Local module to do the conversion to epoch seconds.
Dec 27 '07 #7
Vinod11
4 New Member
Kevin,

The solution is not clear..Please can you elaborate more..
Dec 27 '07 #8
mehj123
55 New Member
Kevin,

The solution is not clear..Please can you elaborate more..
Hi Vinod,

What Kevin is trying to explain is this..

Suppose you have the two dates like this Date1 : 2007/12/26 and Date2: 2007/12/26..

You pass both these to the script thru command line like this

Expand|Select|Wrap|Line Numbers
  1. perl scriptname.pl 2007/12/26 2007/12/26
And in the script, you can retrieve the same using the array @argv, which is used to manipulate the command line arguments
Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my ($date1,$date2) = @ARGV;
  5.  
  6.  

Now the variables $date1 and $date1 will have the dates that you have entered thru the command line..

Now for converting the same to epoch seconds, use the Time:Local module like this
Expand|Select|Wrap|Line Numbers
  1. ($year,$mon, $date) = split/\W+/, $date1;
  2. $epoch_time = timelocal($date,$month,$year);
Now the timelocal() takes the month value as an integer, from 0-11.. so if you have to pass the month "December" you would be passing 11. so you have to modify the $month variable accordingly.
Dec 27 '07 #9
KevinADC
4,059 Recognized Expert Specialist
Kevin,

The solution is not clear..Please can you elaborate more..

Are you a student? Is this school/class/course work?
Dec 27 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
28
by: Steve | last post by:
Hi all How would I find out the average date when given a bunch of dates? For example, I want to find the average length in time from the following dates:...
5
by: Dennis M. Marks | last post by:
After reading section 15.9.1.1 the ECMAScript Language Specifications I see that the date range for the Date function is +/- 100,000,000 days from 01 Jan 1970. This is called an extrapolated...
30
by: Dr John Stockton | last post by:
It has appeared that ancient sources give a method for Numeric Date Validation that involves numerous tests to determine month length; versions are often posted by incomers here. That sort of code...
1
by: Jama | last post by:
I have dates stored as 20040904(yyyymmdd numeric) in DB2 database. I want to get the difference between two dates. I also have the same database in SQL Server database and I am using the following...
5
by: Geoff Jones | last post by:
Hi I have question regarding times and dates in a datatable. I have one table with one column having the date e.g.03/09/04, and another column other the time 08:03:05. The other table has one...
13
by: priyasmita_guha | last post by:
Here is a program- /* PROGRAM: To find the difference between two dates */ #include<dos.h> #include<stdio.h> #include<conio.h> #include<process.h> void valid_date(int,int,int); int...
8
by: Charlie Brookhart | last post by:
I am creating a program that involves having to find the difference between two dates and converting it to a number to be used for calculations. The problem is that the way it is setup, VB is not...
4
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow...
44
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.