[Pharo-dev] Formatting dates

Sven Van Caekenberghe sven at stfx.eu
Wed Oct 30 17:39:42 EDT 2013

Hi Blake,

On 30 Oct 2013, at 21:55, blake <dsblakewatson at gmail.com> wrote:

> Heya, guys--
> Converting a Ruby program to Pharo, and one of the things this program does is write a file out by date. The format is "yyyymmdd" (which is my preferred naming convention). In Ruby I have:
> "%04d" % d.year + "%02d" % d.month + "%02d" % d.day
> In Smalltalk, the closest I can seem to get to this is:
> d year asString, (d monthIndex asString padLeftTo: 2 with: $0), (d dayOfMonth asString padLeftTo: 2 with: $0)
> ...which seems sort verbose. Anyone have any more elegant solutions?

Using the ‘by example’ formatter/parser that is part of the ZTimestamp package (can be loaded using the Configuration Browser), you could say:

  (ZTimestampFormat fromString: '20010203') format: Date today.

Although it is called ZTimestampFormat, it can actually work with Date, Time, DateAndTime as well.


More information about the Pharo-dev mailing list