473,542 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

88 New Member
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 1362
bvdet
2,851 Recognized Expert Moderator Specialist
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
kang jia
88 New Member
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 Recognized Expert Moderator Specialist
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
kang jia
88 New Member
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 Recognized Expert Moderator Specialist
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
kang jia
88 New Member
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

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

Similar topics

6
24789
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 date/time functions the only solution? You can directly subtract 2 DateTime values and a long integer results. What is that number?
23
1915
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 = NewClass() newobj.__dict__.update(object.__dict__) return newobj
2
2238
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 and Int is still DateTime and Int in the XML? Thanks for help.
3
21225
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 DateTimeFormatInfo.FullDateTimePattern property with my custom format string: DateTimeFormatInfo dtfi = ...
8
2936
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 = mssql_query("SELECT date FROM table1") or die(mssql_error());
0
7392
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7330
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7571
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7670
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5867
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3376
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.