-4

I have a string "Hello please change the date from 04/24/2017 by putting month in the middle"

class Paragraph:

    @staticmethod
    def change_date_format(paragraph):
        return None

print(Paragraph.change_date_format('Hello please change the date from 04-24-2017 by putting month in the middle'))

I need to change this to "Hello please change the date from 24/04/2017 by putting month in the middle"

Padmaja Ganesh
  • 81
  • 1
  • 1
  • 7
  • 1
    Not clear what the problem might be – Alex Sep 12 '17 at 11:49
  • Please refer the link [https://stackoverflow.com/questions/14524322/how-to-convert-a-date-string-to-different-format](https://stackoverflow.com/questions/14524322/how-to-convert-a-date-string-to-different-format) – dmp Sep 12 '17 at 11:49
  • I have a sentence which has "Hello today is 07/14/2017". I have to change this to "Hello today is 14/07/2017". @Alex – Padmaja Ganesh Sep 12 '17 at 11:51

2 Answers2

0
import re
def change_date_format(paragraph):
    return re.sub('(\d+)/(\d+)/(\d+)', 
        lambda m: '{}/{}/{}'.format(m.group(2), m.group(1), m.group(3)), 
        paragraph)

This finds a sequence of three groups of digits separated by slashes (like 04/27/2017), then uses format to swap the first and second group.

Błotosmętek
  • 12,717
  • 19
  • 29
-1

datetime can do this:

import datetime
def change_date_format(date):
        return datetime.datetime.strptime(date, '%m/%d/%Y').strftime('%d/%m/%Y')
jss367
  • 4,759
  • 14
  • 54
  • 76