Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8161099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:25:29+00:00 2026-06-06T18:25:29+00:00

In a Ruby 1.9 program, I want to format the current time like Thu

  • 0

In a Ruby 1.9 program, I want to format the current time like Thu 1:51 PM. What format code should I use for the hour of the day (1 in this example)?

Time.now.strftime '%a %I:%M %p' #=> "Thu 01:51 PM"
Time.now.strftime '%a %l:%M %p' #=> "Thu  1:51 PM"

%I has a leading zero (01). %l has a leading space ( 1). I don’t see any other format code for the hour in the strftime documentation. I can’t use .lstrip because the space is in the middle of the string. I could use .gsub(/ +/, " "), but I’m wondering if there’s a less hacky, simpler way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:25:30+00:00Added an answer on June 6, 2026 at 6:25 pm
    Time.now.strftime '%a %-l:%M %p' #=> "Thu 1:51 PM"
    

    Write %-l instead of %l. The - strips leading spaces and zeroes.

    You can use - with the other format codes, too. The Ruby strftime documentation even mentions %-m and %-d, though it fails to mention that you can use - with any code. Thus, %-I would give the same result as %-l. But I recommend %-l, because using l instead of I signifies to me that that you don’t want anything at the beginning – the space it writes looks more accidental.

    You can also see an exhaustive list of Ruby 1.8 strftime format codes, including ones with - and the similar syntaxes _ and 0. It says that Ruby 1.8 on Mac OS X doesn’t support those extended syntaxes, but don’t worry: they work in Ruby 1.9 on my OS X machine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning how to program for the first time, (with Ruby), because I want
I want to print columnar output from a ruby program, but in order to
So all I simply want to do is make a Ruby program that reads
I am able to debug my ruby program. At times, I would want to
I'm writing some code in ruby, and I want to test for the presence
I have the following ruby code, from a learning to program book. I understand
I have a ruby program, and I want to accept the user's made up
I want to write a Ruby program that will fetch all the unique http
I want to create a daemon of a ruby program on Linux. I also
I am working on a multi-threading program in Ruby, just want to make sure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.