-2
MONTHS = ['January', 'February', 'Match', 'April', 'May', 'June',
      'July', 'August', 'September', 'October', 'November', 'December']
def date_convert(s):


    y = s[:4]
    m = int(s[5:-3])
    d = s[8:]
    if m < 10:
        nm = int(m[1:])
        month_name = MONTHS[nm - 1]
    else:
        month_name = MONTHS[m - 1]

    result= ??????
    return result


s = '2000/06/24'
r = date_convert(s)
print(r)

I am working on final exam view(python 3.0 +), and I have been doing practices all day. I suddenly don't know how to put month_name a , y together as a string use 'result' ( result = ???). and then return it to main program. Here is the result I need : convert 2000/06/24 to June 24, 2000. My brain is not working now, please someone helps me. Thank you so much. I cannot use any bulit-in functions. pleases just help me put anything together as a string. Thanks again.

aabbbbcccc
  • 1
  • 1
  • 4
  • Read about `strptime`: https://docs.python.org/3.4/library/datetime.html#strftime-strptime-behavior – DeepSpace Aug 09 '16 at 06:36
  • You mean string formatting? You already have the month name, the year and the day. All you need to do is put those together into a string. A quick Google points to tutorials like http://www.python-course.eu/python3_formatted_output.php. – Martijn Pieters Aug 09 '16 at 06:38
  • Also, `m` and `nm` are *strings*, so `m < 10` is going to fail. You'll want to convert to an integer first, which, incidentally, will make the whole test redundant since you don't need to truncate that `0` if you converted to an integer properly anyway. – Martijn Pieters Aug 09 '16 at 06:39
  • Can anyone just tell me what result equal to please?? It is almost 3 am in Canada now. I want to finish it and I will be taking final exam tomorrow. Thank you very much. – aabbbbcccc Aug 09 '16 at 06:52

3 Answers3

1

Usually you don't parse/reformat/convert dates in Python manually. There are many beautiful functions and even modules for that. The problem is that the date conversion process though may seem simple is complicated and error prone. That's why it is not reimplemented every time, but implemented in so many details as possible in Python core modules (in datetime first of all).

Use strptime to parse the date and strftime to format the date:

>>> import datetime
>>> s = datetime.datetime.strptime("2000/06/24", "%Y/%m/%d")
>>> print s.strftime('%B %d %Y')

The both functions support special formating characters like %Y and %m:

%Y  Year with century as a decimal number.
%m  Month as a decimal number [01,12].
%d  Day of the month as a decimal number [01,31].
%B  Locale’s full month name.

You can find more on it here:

Also, please check this question, that it very close to yours for additional answers:

If it is an excercise, and you must implement the conversion function on your own, that's also fine. Your implementation is almost correct but you've missed some points:

def date_convert(s):
    "Manual date conversion"

    y = s[:4]
    m = int(s[5:-3])
    d = s[8:]
    month_name = MONTHS[m - 1]
    return "%s %s, %s" % (month_name, d, y) 

You don't need if in your function. And you don't need two int conversions, you need only the first one.

You can write the same more briefly:

def date_convert(s):
    "Manual date conversion"

    return "%s %s, %s" % (MONTHS[int(s[5:-3])-1], s[8:], s[:4]) 
Community
  • 1
  • 1
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
0

Try this:

from datetime import datetime

d = "2000/06/24"
date_obj = datetime.strptime(d, "%Y/%m/%d")
formatted_date = datetime.strftime(date_obj, "%B %d %Y")
print(formatted_date)

So you can make d as a variable to be passed as an argument in the function you are using.

strptime(): is used to convert the string of the date to a date object.
strftime(): is used to convert the date object to string.
Prateek
  • 1,538
  • 13
  • 22
0
MONTHS = ['January', 'February', 'Match', 'April', 'May', 'June',
      'July', 'August', 'September', 'October', 'November', 'December']
def date_convert(s):
    y = s[:4]
    m = int(s[5:-3])
    d = s[8:]
    month_name = MONTHS[m-1]

result= month_name + ' ' + d + ' '  + y
return result
s = '2000/06/24'
r = date_convert(s)
print r

I did in python 2 just change print function