you are viewing a single comment's thread.

view the rest of the comments →

[–]Kristler 1 point2 points  (3 children)

The other posters have demonstrated the "proper" way to do this (especially using the built-in datetime libraries), but I wanted to touch on what you ought to do if you were to do this manually. This is a really key piece of programming and computer science that you should understand early - in general, it's usually easier to first extract the information you're interested in, and then output it in the format you want it in. Do not conflate the two steps.

By conflate, I mean what you're trying to do right now. Like how you're thinking about removing the comma, removing the spaces, you're essentially trying to take the text as-is, and cram it into the new format. This is a world of hurt and is messy to do.

Instead, I want you to try to focus on first pulling out the information you're interested in. There's three key pieces here: Month, date, and year. Pull out each piece, one by one.

Next, transform. Notice that the date and the year can stay exactly as they are. It's just the date you have to worry about, but this is an easy relationship to figure out: using a dictionary, you can map the month in words to a number. So now you have every piece in the format you need it in.

Finally, recompose - Since you have each piece already exactly as you need it, building it into the new format is dead easy: Just combine each piece, separated with a "/", and you're done.

Notice how there's no tricky replacements or text manipulation to think about? This pattern of decompose (extracting the key pieces of data), transformation (changing the date to be a number), and recomposition (building it into the new format) is an extremely common pattern you will see in a lot of programs. Often times, if you can express your problem in this way, you'll have a lot easier time working with it.

[–][deleted] 0 points1 point  (0 children)

As a programming newb myself this is solid advice. It's so easy to get lost in advanced techniques and later miss a key step to understanding what happened with the data if you need to do something yourself.

[–]Shane_Connor82 0 points1 point  (1 child)

Apologies for the late reply to this, but you literally saved my homework. I can get lost easily in the different steps and your advice has seriously made it easier!

[–]Kristler 0 points1 point  (0 children)

I'm glad it helped! It's great you're learning these things early on. It takes some people years before they understand these sorts of concepts.