I'm trying to format a date with a day without the leading zero
with %d it works fine, but with leading zero
date_time.strftime("%d/%m/%y")
result: 04/01/11
I googled and found that I should use %e in stead of %d, but doing the following gives me an empty string.
date_time.strftime("%e/%m/%y")
result:
Does this have anything to do with the version of Ruby? I'm using v1.8.7 on a Windows machine. And more important, is there another way to accomplish a day without leading zero (more convenient then gsub)?
DateTime. strptime('2001-02-03T04:05:06+07:00', '%Y-%m-%dT%H:%M:%S%z') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> DateTime. strptime('03-02-2001 04:05:06 PM', '%d-%m-%Y %I:%M:%S %p') #=> #<DateTime: 2001-02-03T16:05:06+00:00 ...>
Use - or # to remove leading 0 datetime. strftime(format) on a date , time , or datetime object to convert a date to a string.
Ruby | Time strftime() function Time#strftime() is a Time class method which returns the time format according to the directives in the given format string. Syntax: Time.strftime() Parameter: Time values. Return: time format according to the directives in the given format string.
If you want to drop the leading zero for month or day just add a minus sign in front of the format, like this:
Date.parse("2014/06/06").strftime("%Y/%-d/%-m")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With