Hi,
It should be simple - but I am trying to find the difference between two datetimes as an integer in seconds. Using pywin32 build 212, python 2.5, Windows XP Pro, MySQL 5.0. - import time
-
import datetime
-
-
def date4string(string):
-
return datetime.date(*time.strptime(string, "%Y-%m-%d %H:%M:%S")[0:3])
-
-
date1="2009-01-01 00:00:00"
-
date2="2009-01-02 00:00:00"
-
d1=date4string(date1)
-
d2=date4string(date2)
-
diff=d2-d1
-
print diff
gives >>> 1 day, 0:00:00
what I want is the answer 86400
Sorry for being an idiot. I have looked at time.mktime; - diff1=time.mktime(diff)
-
print diff1
but that gives
TypeError: argument must be 9-item sequence, not datetime.timedelta
7 29747 bvdet 2,851
Expert Mod 2GB
I suggest using time.mktime(). - def comp_dates(d1, d2):
-
# Date format: %Y-%m-%d %H:%M:%S
-
return time.mktime(time.strptime(d2,"%Y-%m-%d %H:%M:%S"))-\
-
time.mktime(time.strptime(d1, "%Y-%m-%d %H:%M:%S"))
- >>> date1="2009-01-01 00:00:00"
-
>>> date2="2009-01-02 00:00:00"
-
>>> comp_dates(date1, date2)
-
86400.0
-
>>>
Hi,
Thanks for the response. It doesn't help though becuase I have failed to describe the problem properly.
I a writing a script to create a MySQL table using data from an existing set of large tables.
I am pulling two dates (its slightly more complex than that) from the existing tables, and I want to put the difference in seconds back in the resulting table.
I can handle all the MyQSL stuff - I am just miserably stuck on how to get the difference between the two dates - in seconds as an integer.
Regards,
Ben
bvdet 2,851
Expert Mod 2GB
Why did my post not help? You wanted an answer of 86400 and I showed you how to get an answer of 86400.0. Are you not aware that objects can be type cast in Python? - >>> int(comp_dates(date1, date2))
-
86400
-
>>>
I am not making myself very clear.
I don't have strings at all. I have two dates being returned as dates from a MySQL query. They are in date format. I need to be able to subtract one date from another and get a difference in seconds.
I have solved the problem by changing the initial query to return unix_timestamp(date) from the database for each date in question rather than the date. Then subtracting one seconds from epoch from the other seconds from epoch.
I can't belive its not possible to just subtract one date from another and return the number of seconds difference though.
bvdet 2,851
Expert Mod 2GB
OK, maybe it's me that's a bit slow. You have two datetime.date objects, d1 and d2, right? - def comp_dates2(d1, d2):
-
# Date format: %Y-%m-%d %H:%M:%S
-
return time.mktime(time.strptime(d2,"%Y-%m-%d"))-\
-
time.mktime(time.strptime(d1, "%Y-%m-%d"))
-
-
print repr(d1)
-
print repr(d2)
-
print int(comp_dates2(str(d1), str(d2)))
Output: - >>> datetime.date(2009, 1, 1)
-
datetime.date(2009, 1, 2)
-
86400
-
>>>
Another way: - >>> def comp_dates3(d1, d2):
-
... delta = d2-d1
-
... return delta.days*86400+delta.seconds
-
...
-
>>> comp_dates3(d1, d2)
-
86400
-
>>>
I am not sure that I understood, but a solution can be :
Converting to epoch time, doing all the stuff you need and than reconverting it back ?
I hope it helped, regards.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Andrew |
last post by:
1) I have mySQL 3.23 installed. I have a record with two date/times
fields in YYYY-MM-DD HH:MM:SS format. How do I find the difference
between those two date/times? (I would also like to know it...
|
by: DraguVaso |
last post by:
Hi,
I'm new to WebServices, and I'm doing some tests (with a small
VB.NET-application) to know the performance-difference between a WebService
and the 'normal'-way of getting data (just...
|
by: mark |
last post by:
I'm stuck:
In a query I am trying to compare two fields with a date/time data type and a
LongTime format to get the difference in hours (minutes and seconds if
possible). After that I need to...
|
by: John Rivers |
last post by:
Hello everybody,
I just wondered if anybody else has noticed this?
It takes around 6 seconds to start debugging a very simple ASPX page
with VS.NET whereas VB6 takes under 0.5 seconds, even...
|
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...
|
by: HC |
last post by:
Hello, all, I started out thinking my problems were elsewhere but as I
have worked through this I have isolated my problem, currently, as a
difference between MSDE and SQL Express 2005 (I'll just...
|
by: slinky |
last post by:
Anyone know how to calculate the difference between two times
displayed in two textboxes?
I'm starting out with two textboxes: "txtCallTimeBegins" &
"txtCallTimeEnds"
this yielded: 6/7/2007...
|
by: xianwei |
last post by:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int
main ( int argc, char *argv )
{
long i = 10000000L;
clock_t start, end;
double duration;
|
by: orajat |
last post by:
hi,
how do i calculate difference in time in seconds (00:09:05 - 00:09:00 = 300) where start time being a field and the difference in time is calculated in AHT field, ie last record minus previous...
|
by: student4lifer |
last post by:
Hello,
I have 2 time fields dynamically generated in format "m/d/y H:m".
Could someone show me a good function to calculate the time interval
difference in minutes? I played with strtotime() but...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |