4

I can't find a DateTime::Format:: module that outputs the day of the month properly. e.g. Using DateTime->strftime instead of outputting "19 July", I want to output "19th July".

DateTime::Format::Natural::Lang::EN parses dates like that, but I need the opposite - an output formatter.

I don't want to use Date::Calc or any other date module (but would consider non-date modules), and I don't want to have to write it myself.

For reference: DateTime docs, where the strftime patterns don't appear to include "1st, 2nd, 3rd, 4th", etc.

Will Sheppard
  • 3,272
  • 2
  • 31
  • 41
  • 1
    I remembered this is called the "ordinal", and there's some custom code in this [other answer](http://stackoverflow.com/questions/11369907/how-do-i-retrieve-an-integers-ordinal-suffix-in-perl-like-st-nd-rd-th). I'd still like a DateTime format module to achieve this though. – Will Sheppard Jun 19 '14 at 09:06
  • 1
    http://p3rl.org/Lingua::EN::Numbers::Ordinate – choroba Jun 19 '14 at 09:09

1 Answers1

3

Perhaps you could write some kind of custom formatter (and then release it to CPAN!) This gets you some of the way there...

package DateTime::Ordinated;

use strict;
use warnings;

use base 'DateTime';
use Lingua::EN::Numbers::Ordinate;

sub day {
  my $self = shift;

  my $day = $self->SUPER::day;

  return ordinate($day);
}

1;

And to test...

$ perl -MDateTime::Ordinated -E'$d=DateTime::Ordinated->now;say $d->day'
19th
Dave Cross
  • 68,119
  • 3
  • 51
  • 97