PostgreSQL格式的时间戳,以hh:mm am / pm表示

要获取格式为“ h:mm a”的时间戳记/文本(来自js时刻)

For example SELECT to_char(current_timestamp, 'HH12:MI am')

This will give 08:13 am this result, but I need 8:13 am

评论
  • adicta
    adicta 回复

    You can use the fill mode modifier (FM), whose function is to suppress leading zeroes and padding blank:

    SELECT to_char(current_timestamp, 'FMHH12:MI am')
    

    Demo on DB Fiddle:

    | to_char |
    | :------ |
    |上午8:26 |
  • 屎太浓
    屎太浓 回复

    You could use TRIM here:

    SELECT TRIM(LEADING '0' FROM TO_CHAR(CURRENT_TIMESTAMP, 'HH12:MI am'));
    

    Demo

  • 此1不2
    此1不2 回复

    您可以尝试使用“ FM” \“ FX”格式模型修饰符来抑制空白和零,Oracle和PSQL均支持该功能,但我从未在PSQL中尝试过

    SELECT to_char(current_timestamp, 'FMHH12:FXMI am')