What is the simplest way to convert a date formatted 3/2/2004
to 030204
in pure bash, or if not possible, then Python?
I need to at zeros in front of an single digit sectors of the date, remove the parenthesis, and have only the last two characters of the 4 digit year.
I know I could write an extensive Python script that would create an array splitting at /
, and for any single digit arrays I would add a 0
. I don't want to do this because it seems unnecessary. Thanks in advance for any help!
Asked
Active
Viewed 85 times
-8

Skyler Spaeth
- 193
- 1
- 1
- 11
-
4have you tried anything? – depperm Aug 30 '16 at 13:49
-
No. I said in the post that I could write a script that would do it fine, but I think it can be done with one single bash command. – Skyler Spaeth Aug 30 '16 at 13:50
-
strftime(): http://stackoverflow.com/questions/311627/how-to-print-date-in-a-regular-format-in-python – doctorlove Aug 30 '16 at 13:51
-
1@SkylerSpaeth So you're saying that you can do it but you'd rather have someone else do it for you? That's not how SO works. – Harrison Aug 30 '16 at 13:52
-
No, I am looking for a more efficient way than writing a 20 line Python script which I could do, but I doubt I need to because grep, sed, awk, etc could probably be done with one line. – Skyler Spaeth Aug 30 '16 at 13:54
-
1@SkylerSpaeth When you show no attempts at anything it makes it seem as if you've tried nothing and want someone to do it for you. – Harrison Aug 30 '16 at 13:55
-
3I don't understand all the downvotes to the guy... yeah, he didn't try nor researched anything, so what? I've seen a lot of questions like this one to receive many good answers. So even if the question is low quality like this one you can still make good answers. Let's be proactive guys :-) . I mean, you're telling me is fair to give a lot of upvotes to the nonsense question of "have you tried anything?"... cos it's quite obvious he didn't :P – BPL Aug 30 '16 at 14:00
-
Please [edit] your question to show [what you have tried so far](http://whathaveyoutried.com). You should include a [mcve] of the code that you are having problems with, then we can try to help with the specific problem. You should also read [ask]. – Toby Speight Aug 30 '16 at 17:27
2 Answers
2
date -d'11/2/1998' +%m%d%y
110298

P....
- 17,421
- 2
- 32
- 52
-
That worked, thanks! I changed it to `date +%m%d%y` so it is pipable. – Skyler Spaeth Aug 30 '16 at 13:56
2
import datetime
d = datetime.datetime.strptime("11/2/1998", "%d/%m/%Y")
print d.strftime("%d%m%y")

BPL
- 9,632
- 9
- 59
- 117