Mar-11-2018, 12:01 AM
Hi all,
I need your help with my curent code. I have stored the
Example:
Here is what I have stored the datetime in the self.program_end_date list after I have pulled the data from the database:
I feel it is not necessary, but I have got no idea how I can convert from
Can you please show me an example how I can convert from
Thanks in advance
I need your help with my curent code. I have stored the
datetime
object into the list after I have pulled the string from the database. Now I would like to convert it to a string.Example:
datetime.datetime(2018, 3, 12, 0, 0)To this:
12/03/2018 12:00AMIf I want to convert from
datetime
object to a string to make it to show 12/03/2018 12:00AM
, I would have to use something like this:program_stop_date = '20180312000000' stop_time = time.strptime(program_stop_date, '%Y%m%d%H%M%S') stop_time = datetime.datetime.fromtimestamp(time.mktime(stop_time)) program_stop_hours = str(stop_time.hour) program_stop_minutes = str(stop_time.minute) program_stop_days = int(stop_time.day) program_stop_months = int(stop_time.month) program_stop_year = str(stop_time.year) program_stop_hours = int(program_stop_hours) if program_stop_minutes == "0": program_stop_minutes = "00" elif program_stop_minutes == "5": program_stop_minutes = "05" if program_stop_hours >= 0 and program_stop_hours <= 9: program_stop_hours = str(program_stop_hours) program_stop_hours = str("0" + program_stop_hours) if program_stop_days >= 0 and program_stop_days <= 9: program_stop_days = str(program_stop_days) program_stop_days = str("0" + program_stop_days) if program_stop_months >= 0 and program_stop_months <= 9: program_stop_months = str(program_stop_months) program_stop_months = str("0" + program_stop_months) program_stop_hours = int(program_stop_hours) program_stop_days = str(program_stop_days) program_stop_months = str(program_stop_months) if program_stop_hours >= 00 and program_stop_hours <= 12: program_AM_PM = 'AM' else: program_AM_PM = 'PM' program_stop_hours = str(program_stop_hours) program_stop_times = str(program_stop_hours + ':' + program_stop_minutes + program_AM_PM) program_end_time = str(program_stop_days + "/" + program_stop_months + "/" + program_stop_year + " " + program_stop_times)
Here is what I have stored the datetime in the self.program_end_date list after I have pulled the data from the database:
cur.execute('SELECT start_date, stop_date, title, program_id FROM programs WHERE program_id=? LIMIT 10', [program_id]) programs = cur.fetchall() for ind, row in enumerate(programs): program_stop_date = str(row[1]) stop_time = time.strptime(program_stop_date, '%Y%m%d%H%M%S') program_stop_times = time.strftime('%d/%m/%Y %H:%M%p', stop_time) program_stop_time = time.strptime(program_stop_times, '%d/%m/%Y %H:%M%p') self.program_end_time = list() self.program_end_time.append(program_stop_time) The reason I want to convert from `datetime` object to a string, is because I want to convert it from a string to `time.struct_time` object like this: program_stop_times = '12/03/2018 12:00AM' program_stop_time = time.strptime(program_stop_times, '%d/%m/%Y %I:%M%p')
I feel it is not necessary, but I have got no idea how I can convert from
datetime
to a time.struct_time
object. This is the only way I can think of to use the code like I show on the top to convert from datetime
to a string and then to time.struct_time
.Can you please show me an example how I can convert from
datetime
object to a string to make it to show 12/03/2018 12:00AM
or use a better way than I could use?Thanks in advance