2

i am having a small program that gets time from system and dose some arithemetic on it. here is my code:

import time
import datetime
c_time = time.strftime("%H:%M;%S")

# now to convert it into time object i am using 

c_time_converted = datetime.datetime.strptime(c_time,"%H:%M;%S")

print c_time_converted

the output is of the form :

1900-01-01 11:39:40 but i want only the time part when i split and perform other operations on it i get a string returned but i need in time format so that i can perform time arethemetic

i want it in form 11: 39:40

Alex Huszagh
  • 13,272
  • 3
  • 39
  • 67
kshitij singh
  • 363
  • 2
  • 10
  • 19

3 Answers3

3

Just use .time():

from datetime import datetime
c_time = '2015-06-08 23:13:57'
c_time_converted = datetime.strptime(c_time,"%Y-%m-%d %H:%M:%S")
print c_time_converted
print c_time_converted.time()

prints:

2015-06-08 23:13:57
23:13:57

EDIT (addressing a comment regarding datetime)

Here is a simple example of using a difference in datetime:

from datetime import timedelta

c_time_later = '2015-06-08 23:50:21'
c_time_later_converted = datetime.strptime(c_time_later,"%Y-%m-%d %H:%M:%S")
dt = c_time_later_converted - c_time_converted
print dt  # time I was away from stackoverflow

prints: 0:36:24

now we will add dt time back to c_time above and see that we recover c_time_later:

print (c_time_converted + dt).time()

prints: 23:50:21

Or add an hour using timedelta:

print (c_time_converted + timedelta(hours=1)).time() # prints 00:13:57
Scott
  • 6,089
  • 4
  • 34
  • 51
  • This works great but when i use datetime.strptime( someTime,"%H:%M:%S").time() + datetime.strptime( someOtherTime,"%H:%M:%S").time() it gives error – kshitij singh Jun 09 '15 at 06:25
  • 2
    You can add `datetime.timedelta` objects to the datetimeobject. And then switch to `.time()` when you want to print. – camz Jun 09 '15 at 06:33
  • @user3521918 Right, as camz points out, only add a datetime.timedelta to a datetime. See http://stackoverflow.com/questions/13685201/how-to-add-hours-to-current-time-in-python or http://stackoverflow.com/questions/100210/what-is-the-standard-way-to-add-n-seconds-to-datetime-time-in-python – Scott Jun 09 '15 at 06:48
0

I made a minor change in your code. Please check. it will give you time only.

import time
import datetime
c_time = time.strftime("%H:%M:%S")
# now to convert it into time object i am using 
c_time_converted = datetime.datetime.strptime(c_time,"%H:%M:%S")
print c_time_converted.time()
Pankaj
  • 181
  • 1
  • 3
  • but after using .time() arithemetic of two such values is not supported but that is what i am trying to achieve – kshitij singh Jun 09 '15 at 06:31
  • I am totally agree with @Scott. You can use time timedelta object add two times and next get the time form the object. – Pankaj Jun 09 '15 at 07:14
0

Try to this.

import time
import datetime
c_time = time.strftime("%H:%M;%S")

c_time_converted = datetime.datetime.strptime(c_time,"%H:%M;%S")

print c_time_converted.time()

Output :

12:04:31

Get Current Date and Time.

from datetime import datetime
print datetime.now().strftime('%Y-%m-%d %H:%M:%S')

Output:

2015-06-09 12:09:09
Haresh Shyara
  • 1,826
  • 10
  • 13