0

How come this works:

import datetime
now = datetime.datetime.now()
month = '%d' % now.month

print month

But this doesn't?

import datetime
now = datetime.datetime.now()
month = '%m' % now.month

print month

Thanks!

2 Answers 2

3

%m is not a supported format character for the % operator. Here is the list of supported formating characters for this operator

%m is valid when your are using strftime function to build a date string

Sign up to request clarification or add additional context in comments.

1 Comment

@Seamus Longinal: Those directives are specifically to the 'format' argument of time.strftime(format[, t]) docs.python.org/library/time.html#time.strftime
1

'%d' is a format character that insert a "signed integer decimal", '%m' has no such meaning. The possible format characters are listed here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.