python - How to change the string when the string is present -


i need code. have trouble changing strings.

i checking strings if variable gettime3 have string 30 want replace 00. on code, find string 30 replace 0030 wrong. should 00.

here code:

if gettime3 == '11:30pm':    self.getcontrol(346).setlabel('12:00am') elif gettime3 == '12:30pm':      self.getcontrol(346).setlabel('1:00am') else:     ind = gettime3.find(':')     if gettime3[ind+1:ind+3]=='30':        gettime3 = gettime3[:ind]+':00'+gettime3[+2:]        self.getcontrol(346).setlabel(gettime3)      else:         gettime3 = str(int(gettime3[:ind])+1)+':30'+gettime3[+2:]        self.getcontrol(346).setlabel(gettime3) 

what expect 2 special cases, when program finds :, check if 30 present change current hour next hour , make new string am/pm label, example: change 8 9 , replace 30 00 make show 9:00pm. if ending 00 want change 00 30 instead. want add 30 in minute section , again preserves am/pm part. if gettime3 have string 11:30am want change 12:00pm.

can please me how fix 0030 make show 00 instead , add next hour?

with python, slice x[a:b] in slice starting @ a (inclusive), , finishing @ b (exclusive).

so: gettime3[:ind] slice 0 ind exclusive, hours without ":".

and indexes absolute index, not relative. gettime3[+2:] same gettime3[2:], correspond substring starting @ index 2.

what want is:

gettime3 = gettime3[:ind] + ':00' + gettime3[ind + 3:] # or gettime3 = gettime3[:ind + 1] + '00' + gettime3[ind + 3:] 

example:

gettime3 = '08:30pm' ind = gettime3.index(":") gettime3[:ind] + ':00' + gettime3[ind + 3:] # -> '08:00pm' 

edit

if want calculation on time, can use datetime module.

time_fmt = '%i:%m%p' 

is format used represent time '09:30pm', where:

  • %i hour (12-hour clock) zero-padded decimal number.
  • %m minute zero-padded decimal number.
  • %p locale’s equivalent of either or pm.

how add 30 min:

import datetime  time3 = '09:30pm' dt3 = datetime.datetime.strptime(time3, time_fmt) dt3 += datetime.timedelta(minutes=30) time3 = dt3.strftime(time_fmt) 

if want set minutes 0, can do:

dt3 = datetime.datetime.strptime(time3, time_fmt) dt3 = d3.replace(minute=0) 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -