PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Format natural date
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Format natural date

Réponse
 
LinkBack Outils de la discussion
Vieux 10/05/2008, 12h03   #1
John Butler
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Format natural date

Is there a way to format a natural date like May 7, 2008 to 07/05/2008.
Ive tried
Date.strptime("May 7, 2008", '%d/%m/%Y') without success?

Is it possible to this in a one line with ruby?

JB
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 10/05/2008, 13h35   #2
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Format natural date

On 10.05.2008 13:03, John Butler wrote:
> Is there a way to format a natural date like May 7, 2008 to 07/05/2008.
> Ive tried
> Date.strptime("May 7, 2008", '%d/%m/%Y') without success?


You seem to want to _reformat_ a date string. But the code you show
just _parses_ a string into a Date. If you want to reformat your string
you first need to parse it and then format it.

> Is it possible to this in a one line with ruby?


Yes. You're almost there but you need to format your format string so
it matches the date strings that you are trying to parse. Since there
is nowhere a slash in your date string it comes as no surprise that the
format does not match. :-)

Kind regards

robert
  Réponse avec citation
Vieux 10/05/2008, 13h44   #3
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Format natural date

On Sat, May 10, 2008 at 6:03 AM, John Butler <johnnybutler7@gmail.com> wrote:
> Is there a way to format a natural date like May 7, 2008 to 07/05/2008.
> Ive tried
> Date.strptime("May 7, 2008", '%d/%m/%Y') without success?
>
> Is it possible to this in a one line with ruby?
>
> JB


Yes, but I don't know why you would want to.

#strptime builds a Date object from your string with your format.
Then, you must format it.

If you don't need the Date object, I would just perform the magic on
the string itself. Matching month abbreviations to month numbers can
be done with Date::ABBR_MONTHNAMES.

hth,

Todd

  Réponse avec citation
Vieux 10/05/2008, 13h54   #4
John Butler
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Format natural date

Todd Benson wrote:
> On Sat, May 10, 2008 at 6:03 AM, John Butler <johnnybutler7@gmail.com>
> wrote:
>> Is there a way to format a natural date like May 7, 2008 to 07/05/2008.
>> Ive tried
>> Date.strptime("May 7, 2008", '%d/%m/%Y') without success?
>>
>> Is it possible to this in a one line with ruby?
>>
>> JB

>
> Yes, but I don't know why you would want to.
>
> #strptime builds a Date object from your string with your format.
> Then, you must format it.
>
> If you don't need the Date object, I would just perform the magic on
> the string itself. Matching month abbreviations to month numbers can
> be done with Date::ABBR_MONTHNAMES.
>
> hth,
>
> Todd


Ok i see, i didnt explain myself properly, i wanted to turn "May 23,
2008" into a date and then output it as 23/05/2008"

Below works fine.

Date.strptime("May 23, 2008", '%B %d, %Y').strftime("%d/%m/%Y")
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 20h55.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09846 seconds with 12 queries