Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python date formatting without space?

Tags:

python

time

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
like image 595
Hulk Avatar asked Dec 16 '09 10:12

Hulk


People also ask

What does strptime mean?

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.

How do I remove a print space in Python?

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.

How do you remove leading zeros from a date in python?

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.

How do I convert a date to a string in Python?

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.


1 Answers

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")
like image 180
Mithun Arunan Avatar answered Nov 02 '22 15:11

Mithun Arunan