473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Comparing dates problem

Hi,

I am writing a reminder program for our Zimbra email client. One of
the requirements I was given was to automatically increment or
decrement the display to show something like the following:

5 minutes until appointment

or

10 minutes past your appointment
Either way, as each minute goes by, I need to increment it or
decrement it. I am having trouble finding a coherent way to take the
same date and compare just the number of minutes between them to find
the difference. Like if I have an appointment at 9:30 a.m. and the app
is loaded at 8 a.m., I need to know the number of minutes or hours and
minutes until the appointment.

I have looked at the dateutils module and it has many comparison
methods, but they seem to only return the number of days or seconds.

Any ideas would be great!

Mike

May 9 '07 #1
8 1175
On May 10, 7:34 am, kyoso...@gmail.com wrote:
Hi,

I am writing a reminder program for our Zimbra email client. One of
the requirements I was given was to automatically increment or
decrement the display to show something like the following:

5 minutes until appointment

or

10 minutes past your appointment

Either way, as each minute goes by, I need to increment it or
decrement it. I am having trouble finding a coherent way to take the
same date and compare just the number of minutes between them to find
the difference. Like if I have an appointment at 9:30 a.m. and the app
is loaded at 8 a.m., I need to know the number of minutes or hours and
minutes until the appointment.

I have looked at the dateutils module and it has many comparison
methods, but they seem to only return the number of days or seconds.
Ermmm ... what's wrong with

minutes = seconds / 60.0
hours = minutes / 60.0

?

May 9 '07 #2
ky******@gmail.com wrote:
I am writing a reminder program for our Zimbra email client. One of
the requirements I was given was to automatically increment or
decrement the display to show something like the following:

5 minutes until appointment

or

10 minutes past your appointment
Either way, as each minute goes by, I need to increment it or
decrement it. I am having trouble finding a coherent way to take the
same date and compare just the number of minutes between them to find
the difference. Like if I have an appointment at 9:30 a.m. and the app
is loaded at 8 a.m., I need to know the number of minutes or hours and
minutes until the appointment.
Not the most elegant piece of code on earth,
but this piece of code works for me (cut-and-pasted
directly from a working project, so doesn't
*exactly* match your requirement).

<code>
def deltastamp (now, then):

def pluralise (base, n):
if n 1:
return "%d %ss" % (n, base)
else:
return "%d %s" % (n, base)

if now then:
output_format = "%s ago"
delta = now - then
else:
output_format = "in %s"
delta = then - now

days = delta.days
if days <0:
wks, days = divmod (days, 7)
if wks 0:
output = pluralise ("wk", wks)
else:
output = pluralise ("day", days)
else:
mins, secs = divmod (delta.seconds, 60)
hrs, mins = divmod (mins, 60)
if hrs 0:
output = pluralise ("hr", hrs)
elif mins 0:
output = pluralise ("min", mins)
else:
output = pluralise ("sec", secs)

return output_format % output

</code>

TJG
May 10 '07 #3
On May 9, 5:12 pm, John Machin <sjmac...@lexicon.netwrote:
On May 10, 7:34 am, kyoso...@gmail.com wrote:
Hi,
I am writing a reminder program for our Zimbra email client. One of
the requirements I was given was to automatically increment or
decrement the display to show something like the following:
5 minutes until appointment
or
10 minutes past your appointment
Either way, as each minute goes by, I need to increment it or
decrement it. I am having trouble finding a coherent way to take the
same date and compare just the number of minutes between them to find
the difference. Like if I have an appointment at 9:30 a.m. and the app
is loaded at 8 a.m., I need to know the number of minutes or hours and
minutes until the appointment.
I have looked at the dateutils module and it has many comparison
methods, but they seem to only return the number of days or seconds.

Ermmm ... what's wrong with

minutes = seconds / 60.0
hours = minutes / 60.0

?
I'm sure there is a hack for doing something like what you suggest,
but it would be messy. The problem is that I get a datetime object
returned and division really isn't something you can do to one of
those objects. Besides, if I had seconds returned, I would want to
multiply by 60, not divide.

Maybe I misunderstand you.

Mike

May 10 '07 #4
On May 10, 2:37 am, Tim Golden <m...@timgolden.me.ukwrote:
kyoso...@gmail.com wrote:
I am writing a reminder program for our Zimbra email client. One of
the requirements I was given was to automatically increment or
decrement the display to show something like the following:
5 minutes until appointment
or
10 minutes past your appointment
Either way, as each minute goes by, I need to increment it or
decrement it. I am having trouble finding a coherent way to take the
same date and compare just the number of minutes between them to find
the difference. Like if I have an appointment at 9:30 a.m. and the app
is loaded at 8 a.m., I need to know the number of minutes or hours and
minutes until the appointment.

Not the most elegant piece of code on earth,
but this piece of code works for me (cut-and-pasted
directly from a working project, so doesn't
*exactly* match your requirement).

<code>
def deltastamp (now, then):

def pluralise (base, n):
if n 1:
return "%d %ss" % (n, base)
else:
return "%d %s" % (n, base)

if now then:
output_format = "%s ago"
delta = now - then
else:
output_format = "in %s"
delta = then - now

days = delta.days
if days <0:
wks, days = divmod (days, 7)
if wks 0:
output = pluralise ("wk", wks)
else:
output = pluralise ("day", days)
else:
mins, secs = divmod (delta.seconds, 60)
hrs, mins = divmod (mins, 60)
if hrs 0:
output = pluralise ("hr", hrs)
elif mins 0:
output = pluralise ("min", mins)
else:
output = pluralise ("sec", secs)

return output_format % output

</code>

TJG
Thanks for the advice. I think this will work for me with some minor
tweaking. If not, I will post again.

Mike

May 10 '07 #5
On Thu, 2007-05-10 at 11:52 -0700, ky******@gmail.com wrote:
I'm sure there is a hack for doing something like what you suggest,
but it would be messy. The problem is that I get a datetime object
returned and division really isn't something you can do to one of
those objects. Besides, if I had seconds returned, I would want to
multiply by 60, not divide.
If you subtract that datetime object from the current datetime, you'll
get a timedelta object that gives you the number of days and seconds
(and microseconds, if you care) between the two datetimes:
>>import datetime
dt1 = datetime.datetime(2007,5,1,12,0,0)
dt2 = datetime.datetime.now()
delta = dt2 - dt1
delta.days
9
>>delta.seconds
11219

Now, if 60 seconds are one minute, 11219 seconds are how many minutes?
(Answer left as an exercise for the reader.)

Hope this helps,

--
Carsten Haese
http://informixdb.sourceforge.net
May 10 '07 #6
On May 10, 1:52 pm, kyoso...@gmail.com wrote:
On May 9, 5:12 pm, John Machin <sjmac...@lexicon.netwrote:
On May 10, 7:34 am, kyoso...@gmail.com wrote:
Hi,
I am writing a reminder program for our Zimbra email client. One of
the requirements I was given was to automatically increment or
decrement the display to show something like the following:
5 minutes until appointment
or
10 minutes past your appointment
Either way, as each minute goes by, I need to increment it or
decrement it. I am having trouble finding a coherent way to take the
same date and compare just the number of minutes between them to find
the difference. Like if I have an appointment at 9:30 a.m. and the app
is loaded at 8 a.m., I need to know the number of minutes or hours and
minutes until the appointment.
I have looked at the dateutils module and it has many comparison
methods, but they seem to only return the number of days or seconds.
Ermmm ... what's wrong with
minutes = seconds / 60.0
hours = minutes / 60.0
?

I'm sure there is a hack for doing something like what you suggest,
but it would be messy. The problem is that I get a datetime object
returned and division really isn't something you can do to one of
those objects. Besides, if I had seconds returned, I would want to
multiply by 60, not divide.

Maybe I misunderstand you.

Mike
Of course, after posting this, I felt very stupid...

May 10 '07 #7
On May 11, 4:52 am, kyoso...@gmail.com wrote:
On May 9, 5:12 pm, John Machin <sjmac...@lexicon.netwrote:


On May 10, 7:34 am, kyoso...@gmail.com wrote:
Hi,
I am writing a reminder program for our Zimbra email client. One of
the requirements I was given was to automatically increment or
decrement the display to show something like the following:
5 minutes until appointment
or
10 minutes past your appointment
Either way, as each minute goes by, I need to increment it or
decrement it. I am having trouble finding a coherent way to take the
same date and compare just the number of minutes between them to find
the difference. Like if I have an appointment at 9:30 a.m. and the app
is loaded at 8 a.m., I need to know the number of minutes or hours and
minutes until the appointment.
I have looked at the dateutils module and it has many comparison
methods, but they seem to only return the number of days or seconds.
Ermmm ... what's wrong with
minutes = seconds / 60.0
hours = minutes / 60.0
?

I'm sure there is a hack for doing something like what you suggest,
but it would be messy. The problem is that I get a datetime object
returned and division really isn't something you can do to one of
those objects. Besides, if I had seconds returned, I would want to
multiply by 60, not divide.

Maybe I misunderstand you.
Maybe it's mutual -- hack? messy? multiply? Where I come from, 180
seconds is (180 / 60 = 3) minutes. 180 seconds * 60 is 10800 sixtieths-
of-a-second, which appears to be travelling away from a solution to
your problem.

You have *TWO* datetime objects, (say) appt_dt and now_dt.

delta =appt_dt - now_dt # delta is a timedelta object.
# calculate difference in minutes
mins = delta.days * 24 * 60 + delta.seconds // 60

Have you not read Tim Golden's response?

May 10 '07 #8
On May 10, 2:45 pm, John Machin <sjmac...@lexicon.netwrote:
On May 11, 4:52 am, kyoso...@gmail.com wrote:
On May 9, 5:12 pm, John Machin <sjmac...@lexicon.netwrote:
On May 10, 7:34 am, kyoso...@gmail.com wrote:
Hi,
I am writing a reminder program for our Zimbra email client. One of
the requirements I was given was to automatically increment or
decrement the display to show something like the following:
5 minutes until appointment
or
10 minutes past your appointment
Either way, as each minute goes by, I need to increment it or
decrement it. I am having trouble finding a coherent way to take the
same date and compare just the number of minutes between them to find
the difference. Like if I have an appointment at 9:30 a.m. and the app
is loaded at 8 a.m., I need to know the number of minutes or hours and
minutes until the appointment.
I have looked at the dateutils module and it has many comparison
methods, but they seem to only return the number of days or seconds.
Ermmm ... what's wrong with
minutes = seconds / 60.0
hours = minutes / 60.0
?
I'm sure there is a hack for doing something like what you suggest,
but it would be messy. The problem is that I get a datetime object
returned and division really isn't something you can do to one of
those objects. Besides, if I had seconds returned, I would want to
multiply by 60, not divide.
Maybe I misunderstand you.

Maybe it's mutual -- hack? messy? multiply? Where I come from, 180
seconds is (180 / 60 = 3) minutes. 180 seconds * 60 is 10800 sixtieths-
of-a-second, which appears to be travelling away from a solution to
your problem.

You have *TWO* datetime objects, (say) appt_dt and now_dt.

delta =appt_dt - now_dt # delta is a timedelta object.
# calculate difference in minutes
mins = delta.days * 24 * 60 + delta.seconds // 60

Have you not read Tim Golden's response?
Yeah. I said I felt stupid. I'm sorry. I was looking at the problem
from the wrong direction.

Mike

May 10 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: F | last post by:
Hi I have posted the question few days back about problem in inserting the dates in SQL server and thankful to them who replied. That was solved and this is a nice solution....
2
by: Duppypog | last post by:
I'm trying to compare a date stored in a database with today's date using an If statement, but it's not returning true. Example, value in database is 11/5/2003 with today being 11/6/2003. Can...
2
by: Philip Townsend | last post by:
I am having difficulty with a simple routine as follows: public static bool VerifyExpirationDate(DateTime date) { if(date>=DateTime.Now)return true; else return false; } The problem is that...
5
by: JL | last post by:
I need to compare two times. The problem I have is this: In my code I create a time variable using the format statement below: dim firstTime as DateTime fistTime = Format("12:00:00 AM", "T") ...
5
by: Kermit Piper | last post by:
Hello, I am comparing two date values, one from a database and one that has been converted from a hard-coded string into an actual Date type. So far so good. The problem I'm having is that one...
4
by: Working_Girl | last post by:
Hi, I have a database with insurance clients and their dependents (spouses and children). We had a problem in the past with the twins and some of them have been entered with one month...
12
by: colincolehour | last post by:
I am new to Python and am working on my first program. I am trying to compare a date I found on a website to todays date. The problem I have is the website only shows 3 letter month name and the...
4
by: cheryl | last post by:
I am using the PHP.MYSQL and Apache server application in developing my website. I have problem in comparing dates. Website has room reservation, the user will check first the room availability. The...
2
by: dantebothermy | last post by:
Is there a simple way to subtract the time from a datetime field, so when I compare dates, I'm always comparing the dates at 12:00 am? I'd really prefer not to convert all my dates to strings. ...
4
by: jupi13 | last post by:
i have this code..i don't know what where is the error in this one..it says data type mismatch..... Dim Mydate As Date Dim MydateString As String MydateString = "Text1.Text" Mydate =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...

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.