473,549 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Date Difference between two dates

4 New Member

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 !!!

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

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 !!!

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.


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

Here is the Link Date::Calc .

Dec 26 '07 #3
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..Aftrew hich I will be able to do the rest of processing...
Dec 27 '07 #4
971 Recognized Expert Contributor
Please post you code that you have tried and indicate where you are having you problems.

Dec 27 '07 #5
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";
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
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;
use the Time::Local module to do the conversion to epoch seconds.
Dec 27 '07 #7
4 New Member

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

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;

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
4,059 Recognized Expert Specialist

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

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 calculate days, months, and years. This is not for a college course. It's for my own personal genealogy website. I'm stumped about the code. I'm...
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: ---------------------------------------------------- Start Date End Date 01/01/2004 12:50pm 02/01/2004 18:40pm 02/01/2004 13:40pm 02/01/2004 13:57pm...
by: Dennis M. Marks | last post by:
After reading section 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 Gregorian calendar. Since the Gregorian calendar did not begin until 15 Oct 1582 what is the purpose of dates before that date? Wouldn't any computation...
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 seems unnecessarily long. For some while, the following approach has been given here :- function ValidDate(y, m, d) { // m = 0..11 ; y m d...
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 SQL to do the arithmetic in the where clause. SELECT CONVERT(CHAR(10),CONVERT(DATETIME, STR(Date1), 112), 101),...
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 column with the date/time e.g. 09/06/04 13:05:03. What is the easiest way for me to calculate the difference in time, in seconds, between the...
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 getkey(void); void differ_dat(struct date *,struct date *); const int mth={31,28,31,30,31,30,31,31,30,31,30,31};
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 doing anything with the dates that I have selected. I hope that someone here can help me figure out why it is not working what I have to do to make...
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 which calculated the difference in years and days between two dates, and takes leap years into account? I'm calculating the difference in the usual...
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
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.