Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby strftime: Day without leading zero, %e not working

Tags:

ruby

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)?

like image 467
Michael Torfs Avatar asked Mar 23 '11 18:03

Michael Torfs


People also ask

How do I change the date format in Ruby?

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 ...>

How do I remove 0 from a date in python?

Use - or # to remove leading 0 datetime. strftime(format) on a date , time , or datetime object to convert a date to a string.

What is Strftime in Ruby?

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.


1 Answers

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")
like image 89
Colin MacKenzie - III Avatar answered Oct 13 '22 15:10

Colin MacKenzie - III