How to display the following without any space also could we substitute a separator instead of space?
> log_time=datetime.datetime.now()
> print log_time
2009-12-16 16:10:03.558991
DESCRIPTION. The strptime() function converts the character string pointed to by buf to values which are stored in the tm structure pointed to by tm, using the format specified by format. The format is composed of zero or more directives.
strip() Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.
Your answer If you add a hyphen between the % and the letter, you can remove the leading zero. For example %Y/%-m/%-d. This only works on Unix (Linux, OS X), not Windows. On Windows, you would use #, e.g. %Y/%#m/%#d.
To convert Python datetime to string, use the strftime() function. The strftime() method is a built-in Python method that returns the string representing date and time using date, time, or datetime object.
This works like a charm!
Without any separator
import datetime datetime.datetime.now().strftime("%Y%m%d%H%M%S")
Using - as separator
import datetime datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
Using _ as separator
import datetime datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With