-3

I need python code for 'YYYY-MM-DD HH24:MI:SS.FF' format . The result would be like this '2019-07-27 12:07:00.0'

sample code that I tried:

from datetime import datetime as dt

from datetime import timedelta

timestamp=(dt.now() - timedelta(1)).strftime('%Y-%m-%d %HH24:%MI:%SS.%FF')

Output:2019-09-05 10H24:31I:57S.2019-09-05F

Results should looks like 2019-09-05 10:31:57.0

ncica
  • 7,015
  • 1
  • 15
  • 37

2 Answers2

1

Your format string just needs to be adapted - Python takes a single character to tell about the correct output - your repeated characters don't work like that. Here is a corrected code example:

from datetime import datetime as dt

from datetime import timedelta

timestamp=(dt.now() - timedelta(1)).strftime('%Y-%m-%d %H:%M:%S.%f')

As you can see, I just removed some characters and wrote f in lowercase. The format characters that you chose already include padding and 24-hour format.

Example output: '2019-09-05 12:27:45.416157'

For a full list of format characters, please check the linked python documentation.

ingofreyer
  • 1,086
  • 15
  • 27
0

According to this documentation, you should use the following format:

timestamp=(dt.now() - timedelta(1)).strftime('%Y-%m-%d %H:%M:%S.%f')
Sparkofska
  • 1,280
  • 2
  • 11
  • 34