2

I need some help with my 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:00AM

If 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?

4
  • You can use strftime with your desired format, as shown in one of many sources. Commented Mar 11, 2018 at 0:16
  • @mikey how i can convert to strftime after I have stored datetime in the list? Commented Mar 11, 2018 at 0:50
  • Use a for-loop, as suggested in other SO posts where this question has already been answered. Or look at numpy datetime documentation. Commented Mar 11, 2018 at 0:56
  • @mikey i am sorry but i have created the code under a different function so you can see why I have stored the datetime in a list. So how i can convert from datetime to time.struct_time? Commented Mar 11, 2018 at 19:25

1 Answer 1

1

Focusing on your specific example, this is how you can convert a string to a time.struct_time object in a 3-step process:

  1. Convert str to datetime, x in example below.
  2. Convert datetime to date, y.
  3. Convert date to time.struct_time, z.

Below, for convenience, I use dateutil.parser.parse for step 1.

from dateutil import parser
from datetime import datetime

program_stop_times = '12/03/2018 12:00AM'

x = parser.parse(program_stop_times)
y = x.date()
z = y.timetuple()

# time.struct_time(tm_year=2018, tm_mon=12, tm_mday=3, tm_hour=0, tm_min=0,
#                  tm_sec=0, tm_wday=0, tm_yday=337, tm_isdst=-1)

You can, and perhaps should, link these in a single function:

def str_timestruct(x):
    return parser.parse(x).date().timetuple()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.