Ruby strftime: Month without leading zero?
Some versions of strftime
do allow prefixing with minus to format out leading zeros, for eg:
strftime "%-d/%-m/%y"
However this will depend on strftime
on your system. So for consistency I would do something like this instead:
dt = Time.local(2010, 'Sep', 1)
printf "%d/%d/%d", dt.day, dt.month, dt.year
Ruby strftime: Day without leading zero, %e not working
I've had problems before with strftime on Windows not supporting the exact same codes as on unix.
Also, I ran the following on mac os x:
> date_time.strftime("%e/%m/%y")
" 3/03/11"
As you can see, there is a space where the zero was. You still have to process the string after calling strftime.
I'd say, go with the safest bet, in this case %d, and call gsub/trim on it.
How do I write the hour of the time without a leading zero or leading space?
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.
ruby Date.month with a leading zero
There is the possibility of using a formated string output
Examples:
puts sprintf('%02i', 8)
puts '%02i' % 8
%02i
is the format for 2 digits width integer (number) with leading zeros.
Details can be found in the documentation for sprintf
In your specific case with a date, you can just use the Time#strftime
od Date#strftime
method:
require 'time'
puts Time.new(2015,8,1).strftime("%m")
How do I eliminate leading zeroes from my time conversion function?
say you have
time = "00:37:25"
You can use regex to remove the leading zeroes
regex = /^(0*:?)*/
Then you can run sub!
str.sub!(regex, '') #=> 35:25
Format a ruby decimal to string without leading zero (.123)
Solution
You can use :
format('%.3f', value).sub(/^0/, '')
It removes the first character if it is a 0 followed by a dot.
Test
values = [
0.725,
1.5,
0.125,
2.4567,
0,
10
]
values.each do |value|
p format('%.3f', value).sub(/^0/, '')
end
#=>
# ".725"
# "1.500"
# ".125"
# "2.457"
# ".000"
# "10.000"
How do I insert leading zeros in Ruby dateTime minute
try
system["last_checkin"].to_time.strftime('%H:%M')
which will convert it to a Time(which has the date) and then you can use strftime to get the format you want
How can I print a date/time from time::strftime without leading zeros?
Looking the code we can confirm that time
crate does not support the flag -
. Also note that time
crate is on rust-lang-deprecated
user, so it is being deprecated.
That said, I recommend you use the chrono
crate. In addition to supporting the format specifiers you want, the chrono crate also has support for timezones and much more.
let now = chrono::Utc::now();
println!("{}", now.format("%b %-d, %-I:%M").to_string());
No leading zeros for months R
A solution with sub
:
x <- as.Date("2005-09-02")
sub("..0?(.+)-0?(.+)-0?(.+)", "\\3/\\2/\\1", x)
# [1] "2/9/5"
Rails: How to make Date strftime aware of the default locale?
Use the l
(alias for localize
) method instead of raw strftime, like this:
l(date, format: '%B %d, in the year %Y')
See here for more information.
You can also define 'named' formats, a couple of them (short
, long
) are already predefined.
Related Topics
How to Do Standard Deviation in Ruby
Rails Flash Message Remains for Two Page Loads
Postgresql Gem Pg Was Unable to Install
Sorting: Sort Array Based on Multiple Conditions in Ruby
Object.Valid? Returns False But Object.Errors.Full_Messages Is Empty
Creating an Md5 Hash of a Number, String, Array, or Hash in Ruby
How to Make Sign Up Page Be Root Page in Devise
Why Is This String Key in a Hash Converted to a Symbol
Installing Ruby 2.3.X on Ubuntu 18.04 Is Causing an Error by the End of the Installation Process
Package Ruby to a Standalone MAC Os X Application
Which Ruby Memoize Pattern Does Activesupport::Memoizable Refer To
"Rmagick" Gem Installation Issue
Renaming Uploaded Files with Carrierwave
How to Introspect Things in Ruby
Ruby Array Each_Slice_With_Index