471,342 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

how to change this datetime 2007-12-19 21:28:06 format into seconds?

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.
Dec 19 '07 #1
6 1138
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:
Expand|Select|Wrap|Line Numbers
  1. import time
  2.  
  3. def comp_dates(d1, d2):
  4.     # Date format: %Y-%m-%d %H:%M:%S
  5.     return time.mktime(time.strptime(d2, "%Y-%m-%d %H:%M:%S")) - \
  6.            time.mktime(time.strptime(d1, "%Y-%m-%d %H:%M:%S"))
  7.  
  8. dateStr = '2007-12-19 21:28:06'
  9. dateStr1 = '2007-12-20 21:28:06'
  10. diff = comp_dates(dateStr, dateStr1)
  11.  
  12. one_day = 60*60*24
  13.  
  14. if diff > one_day:
  15.     print "You waited too long!"
  16. else:
  17.     print "You made it in time!"
Dec 19 '07 #2
This little function will return the difference between two formatted date strings in seconds:
Expand|Select|Wrap|Line Numbers
  1. import time
  2.  
  3. def comp_dates(d1, d2):
  4.     # Date format: %Y-%m-%d %H:%M:%S
  5.     return time.mktime(time.strptime(d2, "%Y-%m-%d %H:%M:%S")) - \
  6.            time.mktime(time.strptime(d1, "%Y-%m-%d %H:%M:%S"))
  7.  
  8. dateStr = '2007-12-19 21:28:06'
  9. dateStr1 = '2007-12-20 21:28:06'
  10. diff = comp_dates(dateStr, dateStr1)
  11.  
  12. one_day = 60*60*24
  13.  
  14. if diff > one_day:
  15.     print "You waited too long!"
  16. else:
  17.     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:
Expand|Select|Wrap|Line Numbers
  1.  
  2.         dateStr = '2007-12-16 21:28:06'
  3.         x=datetime.datetime.now()
  4.         print x
  5.         one_day=60*60*24
  6.         diff=comp_dates(dateStr,x)
  7.         print diff
  8.         print "LLLLLLLLLLLL"
  9.         if diff > one_day:
  10.             return HttpResponseRedirect ("/unable/")
  11.  
the "print" part is my testing. however, the variable "diff" gives me "none" when i print. can help me with this. thanks so much
Dec 19 '07 #3
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:
Expand|Select|Wrap|Line Numbers
  1.  
  2.         dateStr = '2007-12-16 21:28:06'
  3.         x=datetime.datetime.now()
  4.         print x
  5.         one_day=60*60*24
  6.         diff=comp_dates(dateStr,x)
  7.         print diff
  8.         print "LLLLLLLLLLLL"
  9.         if diff > one_day:
  10.             return HttpResponseRedirect ("/unable/")
  11.  
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:
Expand|Select|Wrap|Line Numbers
  1. >>> now = datetime.datetime.now()
  2. >>> diff1 = comp_dates(dateStr, str(now).split('.')[0])
  3. >>> diff1
  4. -13807.0
  5. >>> 
Some more interaction:
Expand|Select|Wrap|Line Numbers
  1. >>> datetime.datetime.now()
  2. datetime.datetime(2007, 12, 19, 17, 42, 41)
  3. >>> str(datetime.datetime.now())
  4. '2007-12-19 17:42:48.453000'
  5. >>> str(datetime.datetime.now()).split('.')[0]
  6. '2007-12-19 17:43:12'
  7. >>> 
Dec 19 '07 #4
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:
Expand|Select|Wrap|Line Numbers
  1. >>> now = datetime.datetime.now()
  2. >>> diff1 = comp_dates(dateStr, str(now).split('.')[0])
  3. >>> diff1
  4. -13807.0
  5. >>> 
Some more interaction:
Expand|Select|Wrap|Line Numbers
  1. >>> datetime.datetime.now()
  2. datetime.datetime(2007, 12, 19, 17, 42, 41)
  3. >>> str(datetime.datetime.now())
  4. '2007-12-19 17:42:48.453000'
  5. >>> str(datetime.datetime.now()).split('.')[0]
  6. '2007-12-19 17:43:12'
  7. >>> 

it still seems do not work in my side.. still return me the value of none. my entire code is in the following:
Expand|Select|Wrap|Line Numbers
  1. def comp_dates(d1,de2):
  2.     return 
  3.     time.mktime(time.strptime(d2,"%Y-%m-%d %H:%M:%S"))
  4.     time.mktime(time.strptime(d1,"%Y-%m-%d %H:%M:%S"))
  5.  
  6. @login_required    
  7. def deleteCar(request):
  8.  
  9.     if request.user.is_authenticated():
  10.  
  11.         Login = True
  12.         dateStr = '2007-12-16 21:28:06'
  13.         now=datetime.datetime.now()
  14.         print now
  15.         one_day=60*60*24
  16.         diff=comp_dates(dateStr,str(now).split('.')[0])
  17.         print diff
  18.         print "LLLLLLLLLLLL"
  19.         if diff > one_day:
  20.             return HttpResponseRedirect ("/unable/")
  21.  
  22.         if diff < one_day:
  23.             myId=request.session['B_id']
  24.             print myId
  25.             my = Booking1.objects.filter(id__exact= myId)
  26.             my.delete()
  27.         else:
  28.             return HttpResponseRedirect ("/Deny/")
  29.  
  30.     return render_to_response('confirmed_cancel.html')
  31.  
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 :)
Dec 20 '07 #5
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:
Expand|Select|Wrap|Line Numbers
  1. def comp_dates(d1,de2):
  2.     return 
  3.     time.mktime(time.strptime(d2,"%Y-%m-%d %H:%M:%S"))
  4.     time.mktime(time.strptime(d1,"%Y-%m-%d %H:%M:%S"))
  5.  
  6. @login_required    
  7. def deleteCar(request):
  8.  
  9.     if request.user.is_authenticated():
  10.  
  11.         Login = True
  12.         dateStr = '2007-12-16 21:28:06'
  13.         now=datetime.datetime.now()
  14.         print now
  15.         one_day=60*60*24
  16.         diff=comp_dates(dateStr,str(now).split('.')[0])
  17.         print diff
  18.         print "LLLLLLLLLLLL"
  19.         if diff > one_day:
  20.             return HttpResponseRedirect ("/unable/")
  21.  
  22.         if diff < one_day:
  23.             myId=request.session['B_id']
  24.             print myId
  25.             my = Booking1.objects.filter(id__exact= myId)
  26.             my.delete()
  27.         else:
  28.             return HttpResponseRedirect ("/Deny/")
  29.  
  30.     return render_to_response('confirmed_cancel.html')
  31.  
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:
Expand|Select|Wrap|Line Numbers
  1. def comp_dates(d1, d2):
  2.     # Date format: %Y-%m-%d %H:%M:%S
  3.     return time.mktime(time.strptime(d2,"%Y-%m-%d %H:%M:%S"))-\
  4.            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:
Expand|Select|Wrap|Line Numbers
  1. >>> str(datetime.datetime.now())
  2. '2007-12-20 07:51:55.750000'
  3. >>> str(datetime.datetime.now()).split('.')
  4. ['2007-12-20 07:52:05', '437000']
  5. >>> str(datetime.datetime.now()).split('.')[0]
  6. '2007-12-20 07:52:18'
  7. >>> 
Following is the function comp_dates() rewritten using datetime objects:
Expand|Select|Wrap|Line Numbers
  1. def comp_dates1(d1, d2):
  2.     if isinstance(d1, str):
  3.         dt1 = datetime.datetime(*time.strptime(d1, "%Y-%m-%d %H:%M:%S")[:6])
  4.     else: dt1 = d1
  5.     if isinstance(d2, str):
  6.         dt2 = datetime.datetime(*time.strptime(d2, "%Y-%m-%d %H:%M:%S")[:6])
  7.     else: dt2 = d2
  8.     delta = dt2-dt1
  9.     return delta.days*86400+delta.seconds
  10.  
  11. diff2 = comp_dates1(dateStr, dateStr1)
  12. print diff2    
  13.  
  14. dt1 = datetime.datetime(*time.strptime(dateStr, "%Y-%m-%d %H:%M:%S")[:6])
  15. dt2 = datetime.datetime(*time.strptime(dateStr1, "%Y-%m-%d %H:%M:%S")[:6])
  16.  
  17. diff3 = comp_dates1(dt1, dt2)
  18. print diff
Output:
>>> 86401
86401.0
2007-12-19 21:28:06
2007-12-20 08:15:56.093000
38870
>>>
Dec 20 '07 #6
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:
Expand|Select|Wrap|Line Numbers
  1. def comp_dates(d1, d2):
  2.     # Date format: %Y-%m-%d %H:%M:%S
  3.     return time.mktime(time.strptime(d2,"%Y-%m-%d %H:%M:%S"))-\
  4.            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:
Expand|Select|Wrap|Line Numbers
  1. >>> str(datetime.datetime.now())
  2. '2007-12-20 07:51:55.750000'
  3. >>> str(datetime.datetime.now()).split('.')
  4. ['2007-12-20 07:52:05', '437000']
  5. >>> str(datetime.datetime.now()).split('.')[0]
  6. '2007-12-20 07:52:18'
  7. >>> 
Following is the function comp_dates() rewritten using datetime objects:
Expand|Select|Wrap|Line Numbers
  1. def comp_dates1(d1, d2):
  2.     if isinstance(d1, str):
  3.         dt1 = datetime.datetime(*time.strptime(d1, "%Y-%m-%d %H:%M:%S")[:6])
  4.     else: dt1 = d1
  5.     if isinstance(d2, str):
  6.         dt2 = datetime.datetime(*time.strptime(d2, "%Y-%m-%d %H:%M:%S")[:6])
  7.     else: dt2 = d2
  8.     delta = dt2-dt1
  9.     return delta.days*86400+delta.seconds
  10.  
  11. diff2 = comp_dates1(dateStr, dateStr1)
  12. print diff2    
  13.  
  14. dt1 = datetime.datetime(*time.strptime(dateStr, "%Y-%m-%d %H:%M:%S")[:6])
  15. dt2 = datetime.datetime(*time.strptime(dateStr1, "%Y-%m-%d %H:%M:%S")[:6])
  16.  
  17. diff3 = comp_dates1(dt1, dt2)
  18. 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 :)
Dec 21 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Thomas Bartkus | last post: by
23 posts views Thread by digitalorganics | last post: by
2 posts views Thread by Jason Huang | last post: by
3 posts views Thread by Peter Duniho | last post: by
8 posts views Thread by yashiro | last post: by

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.