0

I would like to parse datetime from the command line:

python test.py -time 2017/2/9/ 10:30

But the argparse sees this as 2 seperate strings. I dont want to create another argparse argument just for time, is there a way to keep the date and time as 1 string?

Thanks

  • 1
    quote it. Also, that string is not well formatted to begin with. – TemporalWolf Feb 09 '17 at 20:18
  • With an underscore? Or just wrap the date and time in quotes? That's pretty much all you can do without adding an argument for time, which seems like the cleaner solution – pretzlstyle Feb 09 '17 at 20:18
  • Do I have to strip the quotes later? –  Feb 09 '17 at 20:19
  • @WeiZeng No, your shell should take care of that. – glibdud Feb 09 '17 at 20:22
  • Thats really cool.... python is too smart for me. Thanks –  Feb 09 '17 at 20:23
  • Consider a format like: `'1970-01-01T16:40:00'` which joins date and time into one string. Or any other single string format that your `strptime` can handle: `datetime.datetime.strptime("21/11/06T16:30", "%d/%m/%yT%H:%M")` – hpaulj Feb 09 '17 at 20:39
  • `nargs=2` will let you enter two strings for this argument. You could join them later when converting to a `datetime` object. – hpaulj Feb 09 '17 at 20:42

0 Answers0