本文介绍了Ruby strftime:没有前导零的月份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ruby的 strftime 有没有前导零的月份的格式?

Does Ruby's strftime have a format for the month without a leading zero?

我发现%e 获得一天没有领先的零,但没有运气与这个月。

I found %e for getting the day without the leading zero, but not having any luck with the month.

最终想要一个日期格式化喜欢: 9/1/2010

Ultimately wanting a date formatted like: 9/1/2010

推荐答案

某些版本的 strftime 允许使用减去的前缀格式化前导零,例如:

Some versions of strftime do allow prefixing with minus to format out leading zeros, for eg:

strftime "%-d/%-m/%y"

这将取决于您的系统上的 strftime 。所以为了一致性,我会做这样的事情:

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

/ I3az /

这篇关于Ruby strftime:没有前导零的月份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 18:15