Month Validation with Arrow? #1071
Replies: 4 comments 4 replies
-
Hi @Swizzler121, if I'm understanding your question correctly then here's how to do what you want. Use the >>> s="2020-9-17"
>>> arrow.get(s, "YYYY-M-DD")
<Arrow [2020-09-17T00:00:00+00:00]> https://arrow.readthedocs.io/en/latest/#supported-tokens For the written month you could convert like this. >>> dt=arrow.get("March", "MMMM")
>>> dt
<Arrow [0001-03-01T00:00:00+00:00]>
>>> dt.month
3
>>> dt.format("MM")
'03' arrow also wraps >>> arrow.Arrow.strptime("2", "%m")
<Arrow [1900-02-01T00:00:00+00:00]> |
Beta Was this translation helpful? Give feedback.
-
Can you provide the full error message output? Try doing this. parser.add_argument(
"-m","--month",
nargs='+',
type=lambda d: arrow.get(d, 'MM').format('MM').datetime,
help="specify a month (integer)"
) |
Beta Was this translation helpful? Give feedback.
-
The full error before:
The error now:
Just to reiterate, the reason I'm feeding it 1 digit dates instead of two is I want the system to be able to interpret both, and maybe even written months if possible, The datetime version I posted in my first post takes a 1 or 2 digit number, checks if its between 1-12, then if it is passes it to the program, if not, it errors out. The arrow version works if the month values are 2 digits. |
Beta Was this translation helpful? Give feedback.
-
Ahh sorry I gave you the wrong code, try this. parser.add_argument(
"-m","--month",
nargs='+',
type=lambda d: arrow.Arrow.strptime(d, '%m').datetime,
help="specify a month (integer)"
) This should give you exactly the same return value for |
Beta Was this translation helpful? Give feedback.
-
I'm trying to replace datetime in my python, everything so far has worked beautifully, but I have an argument parser that I have a line that checks an input month to see if it's valid. the arrow version works fine if a user uses a 2 digit month, but if you use a single digit month, it says it's not valid. the datetime version converts single digit months to 2 digit before validation, so it works fine if a user gives a single or 2 digit month. I was hoping arrow could go even further and convert a written month to a 2 digit month, but I can't figure out how to do this.
the datetime variant:
parser.add_argument("-m","--month", type=lambda d: datetime.strptime(d, '%m'), help="specify a month (integer)")
Beta Was this translation helpful? Give feedback.
All reactions