hi
Currently i am doing a website for users to book cars online, however, when inserting the time into database is in the format of 2007-12-19 21:28:06 which is fine. when i do my application into next phrase, the problem come out, as i will only allow user to update their car booking within 24 hours. then i am thinking of change all the time into seconds. if it is bigger than 24 hours which will be in the format of seconds, it will stop user to cancel their booking. At the moment, i am not sure how to convert 2007-12-19 21:28:06 into seconds. for 24 hours,it just need to times 3600, it will change into seconds. can anyone help me with function?? how can i convert it. thanks in advance for any help.
it would be a bit urgent as time is very tight for this project. thanks so much.
6 1357 bvdet 2,851
Expert Mod 2GB
hi
Currently i am doing a website for users to book cars online, however, when inserting the time into database is in the format of 2007-12-19 21:28:06 which is fine. when i do my application into next phrase, the problem come out, as i will only allow user to update their car booking within 24 hours. then i am thinking of change all the time into seconds. if it is bigger than 24 hours which will be in the format of seconds, it will stop user to cancel their booking. At the moment, i am not sure how to convert 2007-12-19 21:28:06 into seconds. for 24 hours,it just need to times 3600, it will change into seconds. can anyone help me with function?? how can i convert it. thanks in advance for any help.
it would be a bit urgent as time is very tight for this project. thanks so much.
This little function will return the difference between two formatted date strings in seconds: - import time
-
-
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"))
-
-
dateStr = '2007-12-19 21:28:06'
-
dateStr1 = '2007-12-20 21:28:06'
-
diff = comp_dates(dateStr, dateStr1)
-
-
one_day = 60*60*24
-
-
if diff > one_day:
-
print "You waited too long!"
-
else:
-
print "You made it in time!"
This little function will return the difference between two formatted date strings in seconds: - import time
-
-
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"))
-
-
dateStr = '2007-12-19 21:28:06'
-
dateStr1 = '2007-12-20 21:28:06'
-
diff = comp_dates(dateStr, dateStr1)
-
-
one_day = 60*60*24
-
-
if diff > one_day:
-
print "You waited too long!"
-
else:
-
print "You made it in time!"
Really thanks so much for your kind reply, if possible could me let me know what is the use of strptime function? it seems to me that it maps into the format of "2007-12-19 21:28:06" if i did not interpret wrongly. but at the moment i am not very clear how it works.
when i print diff, it gives me the value of none, thus it is unable for me to compare between diff > one_day...i am not sure where is wrong, can kindly help me with this. thanks so much. main part of my code is in the following: -
-
dateStr = '2007-12-16 21:28:06'
-
x=datetime.datetime.now()
-
print x
-
one_day=60*60*24
-
diff=comp_dates(dateStr,x)
-
print diff
-
print "LLLLLLLLLLLL"
-
if diff > one_day:
-
return HttpResponseRedirect ("/unable/")
-
the "print" part is my testing. however, the variable "diff" gives me "none" when i print. can help me with this. thanks so much
bvdet 2,851
Expert Mod 2GB
Really thanks so much for your kind reply, if possible could me let me know what is the use of strptime function? it seems to me that it maps into the format of "2007-12-19 21:28:06" if i did not interpret wrongly. but at the moment i am not very clear how it works.
when i print diff, it gives me the value of none, thus it is unable for me to compare between diff > one_day...i am not sure where is wrong, can kindly help me with this. thanks so much. main part of my code is in the following: -
-
dateStr = '2007-12-16 21:28:06'
-
x=datetime.datetime.now()
-
print x
-
one_day=60*60*24
-
diff=comp_dates(dateStr,x)
-
print diff
-
print "LLLLLLLLLLLL"
-
if diff > one_day:
-
return HttpResponseRedirect ("/unable/")
-
the "print" part is my testing. however, the variable "diff" gives me "none" when i print. can help me with this. thanks so much
datetime.datetime.now() creates a datetime object and time.strptime() returns a struct_time object. They are somewhat compatible. The function should work if you pass the datetime object to the function like this: - >>> now = datetime.datetime.now()
-
>>> diff1 = comp_dates(dateStr, str(now).split('.')[0])
-
>>> diff1
-
-13807.0
-
>>>
Some more interaction: - >>> datetime.datetime.now()
-
datetime.datetime(2007, 12, 19, 17, 42, 41)
-
>>> str(datetime.datetime.now())
-
'2007-12-19 17:42:48.453000'
-
>>> str(datetime.datetime.now()).split('.')[0]
-
'2007-12-19 17:43:12'
-
>>>
datetime.datetime.now() creates a datetime object and time.strptime() returns a struct_time object. They are somewhat compatible. The function should work if you pass the datetime object to the function like this: - >>> now = datetime.datetime.now()
-
>>> diff1 = comp_dates(dateStr, str(now).split('.')[0])
-
>>> diff1
-
-13807.0
-
>>>
Some more interaction: - >>> datetime.datetime.now()
-
datetime.datetime(2007, 12, 19, 17, 42, 41)
-
>>> str(datetime.datetime.now())
-
'2007-12-19 17:42:48.453000'
-
>>> str(datetime.datetime.now()).split('.')[0]
-
'2007-12-19 17:43:12'
-
>>>
it still seems do not work in my side.. still return me the value of none. my entire code is in the following: -
def comp_dates(d1,de2):
-
return
-
time.mktime(time.strptime(d2,"%Y-%m-%d %H:%M:%S"))
-
time.mktime(time.strptime(d1,"%Y-%m-%d %H:%M:%S"))
-
-
@login_required
-
def deleteCar(request):
-
-
if request.user.is_authenticated():
-
-
Login = True
-
dateStr = '2007-12-16 21:28:06'
-
now=datetime.datetime.now()
-
print now
-
one_day=60*60*24
-
diff=comp_dates(dateStr,str(now).split('.')[0])
-
print diff
-
print "LLLLLLLLLLLL"
-
if diff > one_day:
-
return HttpResponseRedirect ("/unable/")
-
-
if diff < one_day:
-
myId=request.session['B_id']
-
print myId
-
my = Booking1.objects.filter(id__exact= myId)
-
my.delete()
-
else:
-
return HttpResponseRedirect ("/Deny/")
-
-
return render_to_response('confirmed_cancel.html')
-
i am using Django framework, but it is using python programming language. i really got no idea where went wrong. can help me with this.. thanks so much.
hmm, my i ask you what is the meaning of [0] in the codediff=comp_dates(dateStr,str(now).split('.')[0])...thanks :)
bvdet 2,851
Expert Mod 2GB
it still seems do not work in my side.. still return me the value of none. my entire code is in the following: -
def comp_dates(d1,de2):
-
return
-
time.mktime(time.strptime(d2,"%Y-%m-%d %H:%M:%S"))
-
time.mktime(time.strptime(d1,"%Y-%m-%d %H:%M:%S"))
-
-
@login_required
-
def deleteCar(request):
-
-
if request.user.is_authenticated():
-
-
Login = True
-
dateStr = '2007-12-16 21:28:06'
-
now=datetime.datetime.now()
-
print now
-
one_day=60*60*24
-
diff=comp_dates(dateStr,str(now).split('.')[0])
-
print diff
-
print "LLLLLLLLLLLL"
-
if diff > one_day:
-
return HttpResponseRedirect ("/unable/")
-
-
if diff < one_day:
-
myId=request.session['B_id']
-
print myId
-
my = Booking1.objects.filter(id__exact= myId)
-
my.delete()
-
else:
-
return HttpResponseRedirect ("/Deny/")
-
-
return render_to_response('confirmed_cancel.html')
-
i am using Django framework, but it is using python programming language. i really got no idea where went wrong. can help me with this.. thanks so much.
hmm, my i ask you what is the meaning of [0] in the codediff=comp_dates(dateStr,str(now).split('.')[0])...thanks :)
The problem is in your function comp_dates(). You have return on one line by itself, which immediately returns None. Pay colse attention to the format and indentation of the following code: - 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"))
A datetime object includes milliseconds. When a datetime object is converted to a string as in str(dt_obj), the string looks like '2007-12-19 17:42:48.453000'. The code str(now).split('.')[0] splits the string on the decimal point in the seconds part and returns the first item in the list. Example: - >>> str(datetime.datetime.now())
-
'2007-12-20 07:51:55.750000'
-
>>> str(datetime.datetime.now()).split('.')
-
['2007-12-20 07:52:05', '437000']
-
>>> str(datetime.datetime.now()).split('.')[0]
-
'2007-12-20 07:52:18'
-
>>>
Following is the function comp_dates() rewritten using datetime objects: - def comp_dates1(d1, d2):
-
if isinstance(d1, str):
-
dt1 = datetime.datetime(*time.strptime(d1, "%Y-%m-%d %H:%M:%S")[:6])
-
else: dt1 = d1
-
if isinstance(d2, str):
-
dt2 = datetime.datetime(*time.strptime(d2, "%Y-%m-%d %H:%M:%S")[:6])
-
else: dt2 = d2
-
delta = dt2-dt1
-
return delta.days*86400+delta.seconds
-
-
diff2 = comp_dates1(dateStr, dateStr1)
-
print diff2
-
-
dt1 = datetime.datetime(*time.strptime(dateStr, "%Y-%m-%d %H:%M:%S")[:6])
-
dt2 = datetime.datetime(*time.strptime(dateStr1, "%Y-%m-%d %H:%M:%S")[:6])
-
-
diff3 = comp_dates1(dt1, dt2)
-
print diff
Output: >>> 86401
86401.0
2007-12-19 21:28:06
2007-12-20 08:15:56.093000
38870
>>>
The problem is in your function comp_dates(). You have return on one line by itself, which immediately returns None. Pay colse attention to the format and indentation of the following code: - 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"))
A datetime object includes milliseconds. When a datetime object is converted to a string as in str(dt_obj), the string looks like '2007-12-19 17:42:48.453000'. The code str(now).split('.')[0] splits the string on the decimal point in the seconds part and returns the first item in the list. Example: - >>> str(datetime.datetime.now())
-
'2007-12-20 07:51:55.750000'
-
>>> str(datetime.datetime.now()).split('.')
-
['2007-12-20 07:52:05', '437000']
-
>>> str(datetime.datetime.now()).split('.')[0]
-
'2007-12-20 07:52:18'
-
>>>
Following is the function comp_dates() rewritten using datetime objects: - def comp_dates1(d1, d2):
-
if isinstance(d1, str):
-
dt1 = datetime.datetime(*time.strptime(d1, "%Y-%m-%d %H:%M:%S")[:6])
-
else: dt1 = d1
-
if isinstance(d2, str):
-
dt2 = datetime.datetime(*time.strptime(d2, "%Y-%m-%d %H:%M:%S")[:6])
-
else: dt2 = d2
-
delta = dt2-dt1
-
return delta.days*86400+delta.seconds
-
-
diff2 = comp_dates1(dateStr, dateStr1)
-
print diff2
-
-
dt1 = datetime.datetime(*time.strptime(dateStr, "%Y-%m-%d %H:%M:%S")[:6])
-
dt2 = datetime.datetime(*time.strptime(dateStr1, "%Y-%m-%d %H:%M:%S")[:6])
-
-
diff3 = comp_dates1(dt1, dt2)
-
print diff
Output:>>> 86401
86401.0
2007-12-19 21:28:06
2007-12-20 08:15:56.093000
38870
>>>
really thanks for your kind help and i have understood how it works as well. it works fine now in my website. Thanks! haha :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Thomas Bartkus |
last post by:
MySQL Version 4.0.20 on a Linux server.
How does one get the elapsed time between (2) DateTime values?
I need the answer to the nearest minute.
Is upgrading to Ver 5 with its more robust...
|
by: digitalorganics |
last post by:
How can an object replace itself using its own method? See the
following code:
class Mixin:
def mixin(object, *classes):
NewClass = type('Mixin', (object.__class__,) + classes, {})
newobj =...
|
by: Jason Huang |
last post by:
Hi,
In my C# windows form application, I can write a dataset to an XML file.
But everything in that XML are String type.
Is is possible to write a dataset to an XML file, so that the DateTime...
|
by: Peter Duniho |
last post by:
I'm sure there's a good explanation for this, but I can't figure it out.
I tried using DateTime.Parse() with a custom DateTimeFormatInfo instance,
in which I'd replaced the...
|
by: yashiro |
last post by:
Hello
i Have a database in SQLserver.
I am using php to connect to it and to retrieve data from that
database.
I have a table with a column (DATATIME)
where i do :
$result =...
|
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...
|
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...
|
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: 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: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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: 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...
|
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...
| |