-2

what is the problem in this- string=strftime("%H:%M:%S %P") when i run the code it called me

string=strftime("%H:%M:%S %P")
ValueError: Invalid format string

what is the solution please tell me.

buran
  • 13,682
  • 10
  • 36
  • 61
  • 2
    @IainShelvington Yes, it's valid as long as the C version of his system considers valid: [`strftime(3)`](https://man7.org/linux/man-pages/man3/strftime.3.html). From the [Python docs](https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior): "The full set of format codes supported varies across platforms, because Python calls the platform C library’s strftime() function, and platform variations are common. To see the full set of format codes supported on your platform, consult the strftime(3) documentation." So he should call `man 3 strftime` and see if it's available. – Enzo Ferber Sep 17 '21 at 12:19
  • 1
    @IainShelvington On my system, Python 3.8.10, GCC 9.4.0, `from time import strftime`, the result of calling with "%P" is lower-case "am", and the result with "%p" is uppercase "AM". As the manual says, it varies according to implementations... – Enzo Ferber Sep 17 '21 at 12:30
  • @EnzoFerber: is there a way to specify a cross-platform date format string? –  Jun 14 '22 at 17:31

1 Answers1

0

This works great, although without %p

from datetime import datetime
n = datetime.now()      


string=n.strftime("%H:%M:%S")
print(string)


To understand why it might not work with %p read this: https://stackoverflow.com/a/38863256/3847725

  • Try testing your code on this online interpreter https://www.programiz.com/python-programming/online-compiler/ And see if your code works, it might be locale specific is what i'm trying to say. – Victor Loke Chapelle Hansen Sep 17 '21 at 12:33