473,326 Members | 2,588 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,326 software developers and data experts.

subtract dates with time module

I'm trying to get the difference in dates using the time module rather
than datetime because I need to use strptime() to convert a date and
then find out how many weeks and days until that date. I'm a beginner
so any help would be appreciated. Here is the code:

def OBweeks(ptID):
qry = 'SELECT short_des FROM problems WHERE patient_ID = %s;' %
(ptID)
results = EMR_utilities.getAllData(qry)
for items in results:
r = re.search('\d\d\d\d-\d\d-\d\d', items)
if r:
d = time.strptime(r.group(), "%Y-%m-%d') -
time.localtime()
weeks, days = divmod(d.days, 7)
return '%s %s/7 weeks' % (weeks, days)

This isn't working. I'm getting "unsupported operand type for -:
'time.struct_time' and 'time.struct_time" error.

Thanks, Mike
Mar 26 '08 #1
2 5385
barronmo wrote:
I'm trying to get the difference in dates using the time module rather
than datetime because I need to use strptime() to convert a date and
then find out how many weeks and days until that date. I'm a beginner
so any help would be appreciated. Here is the code:
Use strptime to create time-tuples, and use the fields in there to create
datetime-objects. Then do the math with them.

Diez
Mar 27 '08 #2
barronmo wrote:
I'm trying to get the difference in dates using the time module rather
than datetime because I need to use strptime() to convert a date and
then find out how many weeks and days until that date.
datetime.datetime.strptime was introduced in Python 2.5; what version
are you using?

If you really want to get to datetime, here's a quick bridge:
>>import time, datetime
def mystrptime(astr, format):
.... return datetime.datetime(*time.strptime(astr, format)[:6])
....
>>mystrptime('2008-03-31', '%Y-%m-%d')
datetime.datetime(2008, 3, 31, 0, 0)

I'm a beginner
so any help would be appreciated. Here is the code:

def OBweeks(ptID):
qry = 'SELECT short_des FROM problems WHERE patient_ID = %s;' %
(ptID)
results = EMR_utilities.getAllData(qry)
for items in results:
r = re.search('\d\d\d\d-\d\d-\d\d', items)
if r:
d = time.strptime(r.group(), "%Y-%m-%d') -
You have " at the start and ' at the end of what's supposed to be a
string constant. That's a syntax error; it won't run. Please don't serve
up what you thought you might have run -- use copy/paste.

In this particular case, you can just use time.mktime to convert a time
tuple into days since the epoch.

# untested
days = time.mktime(time.strptime(r.group(), "%Y-%m-%d")) -
int(time.mktime(time.localtime()))
weeks, days = divmod(days, 7)
time.localtime()
weeks, days = divmod(d.days, 7)
return '%s %s/7 weeks' % (weeks, days)

This isn't working. I'm getting "unsupported operand type for -:
'time.struct_time' and 'time.struct_time" error.
It *is* working. That is the correct result of the code that you
executed. There is is nothing in the time docs to suggest that
attempting to subtract time tuples produces anything useful.

HTH,
John
Mar 27 '08 #3

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

Similar topics

3
by: Ken | last post by:
$expirationdate = 4/15/2005 $startdate = 3/1/2005 I would like to determine the number of days between the above dates. My approach: $startdate = strtotime($startdate); // change to a string...
11
by: Amy G | last post by:
I have seen something about this beofore on this forum, but my google search didn't come up with the answer I am looking for. I have a list of tuples. Each tuple is in the following format: ...
8
by: dlx_son | last post by:
Here is the code so far <form name="thisform"> <h3>Enter time to add to or subtract from:</h3> (If not entered, current time will be used)<br> Day: <input name="d1" alt="Day of month"...
5
by: cvisal | last post by:
Hi all Im working on productivity calculations (Time calculations) and need some help in coding. Database Tool:MS-Access 2003. The general operator punch-in time is 5:30 AM and the punch-out...
4
by: meltedown | last post by:
I can't see what I'm doing wrong. I'm subtracting 60*60*24 from a unix time stamp and the result is 23 hours earlier, not 24. Start with a unix time stamp: $unixtime=1144018006; convert it to...
8
by: Remington | last post by:
I am using windows 2000pro with access 2000. I am trying to make a database for our HR department, that would allow our HR Director to type in an employee's ID number into a form and then select...
10
by: dan | last post by:
Am i breaking any rules when I loop dates like // Determine Memorial Day intFlag = 0; memDayHol = new Date (currentYear, 4, 31); while (intFlag == 0) { if (memDayHol.getDay() == 1) {intFlag...
6
by: lptl | last post by:
I know the title is misleading. I am working on a class project where we are trying to setup a notification system written in PHP and using an MySQL database. We are trying to set-up a notification...
6
by: W. eWatson | last post by:
That's the question in Subject. For example, the difference between 08/29/2008 and 09/03/2008 is +5. The difference between 02/28/2008 and 03/03/2008 is 4, leap year--extra day in Feb. I'm really...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.