IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Delphi Discussion :

Date formatée vers Date (StrToDate "amélioré")


Sujet :

Langage Delphi

  1. #1
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 036
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 036
    Points : 40 942
    Points
    40 942
    Billets dans le blog
    62
    Par défaut Date formatée vers Date (StrToDate "amélioré")
    Bonjour,

    je tente peut être de réinventer la roue , dans ce cas merci de me le signaler.

    Je voudrai à partir d'une date formatée (par exemple sous la forme ddd dd mmm yyyy soit Lun. 2 Juil. 2018) récupéré la dite date

    j'ai donc écrit cette fonction
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    function FormatedDateToDate(Format,FormatedDate : String) : TDate;
       var i : Word;
       begin
    //   try
         for I := 1 to 7 do
         begin
          S:=StringReplace(S,FormatSettings.ShortDayNames[i],'',[rfReplaceAll,rfIgnoreCase]);
          S:=StringReplace(S,FormatSettings.LongDayNames[i],'',[rfReplaceAll,rfIgnoreCase]);
         end;
         for I := 1 to 12 do
           begin
             S:=StringReplace(S,FormatSettings.ShortMonthNames[i],FormatSettings.DateSeparator+i.ToString+FormatSettings.DateSeparator,[rfReplaceAll,rfIgnoreCase]);
             S:=StringReplace(S,FormatSettings.LongMonthNames[i],FormatSettings.DateSeparator+i.ToString+FormatSettings.DateSeparator,[rfReplaceAll,rfIgnoreCase]);
           end;
     
         Result:=StrToDateDef(S,-1);
    //    except
    //    end;
       end;
    si elle fonctionne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    procedure TForm15.Button1Click(Sender: TObject);
    var S,Format : String;
        ADate : TDate;
    begin
     Format:='ddd dd mmm yyyy';
     S:=FormatDateTime(Format,Date);
     Showmessage(s);
     ADate:=FormatedDateToDate(Format,S);
     if ADate>=0 then Label1.Text:=FormatDateTime(FormatSettings.ShortDateFormat,ADate);
    end;
    je voudrai quand même l'étendre à plus de possibilités
    quelques tests avec d'autres format fonctionnent aussi 'dd mmm yy','dd/mm/yy','dddd dd mmmm e','dd/mm' ...
    et certains formats comme 'ddd dd mmm' non (celui là c'est normal, j'ai un / en trop à la fin)

    mais elles ont toutes l'obligation d'être sous la forme jour mois <an>
    J'ai fait quelques tentatives avec StrToDate(string,TFormatSettings) mais sans succès (si quelqu'un peut m'expliquer )

    en gros j'ai testé ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    AFormatSettings : TFormatSettings;
    ADate : TDate;
    ...
    AFormatSettings:=TFormatSettings.Create();
    AFormatSettings.LongDateFormat:='ddd dd mmm yyyy'; 
    ADate:=StrToDate(FormatDateTime(AFormatSettings.LongDateFormat,Date);
    MVP Embarcadero
    Delphi installés : D3,D7,D2010,XE4,XE7,D10 (Rio, Sidney), D11 (Alexandria), D12 (Athènes)
    SGBD : Firebird 2.5, 3, SQLite
    générateurs États : FastReport, Rave, QuickReport
    OS : Window Vista, Windows 10, Windows 11, Ubuntu, Androïd

  2. #2
    Expert éminent sénior
    Avatar de ShaiLeTroll
    Homme Profil pro
    Développeur C++\Delphi
    Inscrit en
    Juillet 2006
    Messages
    13 449
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Développeur C++\Delphi
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2006
    Messages : 13 449
    Points : 24 856
    Points
    24 856
    Par défaut
    C'est con mais l'espace ne pourra jamais être un séparateur, tout espace est retiré durant l'analyse ce qui rend le découpage impossible

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
     
    function SimplifyFormatedDate(const ADate: string; var AFormat: string): string;
    var
      I: Integer;
    begin
      Result := ADate;
      for I := 1 to 7 do
      begin
        Result := StringReplace(Result, FormatSettings.ShortDayNames[I], '', [rfReplaceAll, rfIgnoreCase]);
        Result := StringReplace(Result, FormatSettings.LongDayNames[I], '', [rfReplaceAll, rfIgnoreCase]);
      end;
      Result := StringReplace(Result, '  ', ' ', [rfReplaceAll, rfIgnoreCase]);
      for I := 1 to 12 do
      begin
        Result := StringReplace(Result, FormatSettings.ShortMonthNames[I], Format('%.2d', [I]), [rfReplaceAll, rfIgnoreCase]);
        Result := StringReplace(Result, FormatSettings.LongMonthNames[I], Format('%.2d', [I]), [rfReplaceAll, rfIgnoreCase]);
      end;
      Result := StringReplace(Trim(Result), ' ', FormatSettings.DateSeparator, [rfReplaceAll]);
     
      AFormat := StringReplace(AFormat, 'dddd', '', [rfReplaceAll, rfIgnoreCase]);
      AFormat := StringReplace(AFormat, 'ddd', '', [rfReplaceAll, rfIgnoreCase]);
      AFormat := StringReplace(AFormat, '  ', ' ', [rfReplaceAll, rfIgnoreCase]);
      AFormat := StringReplace(AFormat, 'mmmm', 'mm', [rfReplaceAll, rfIgnoreCase]);
      AFormat := StringReplace(AFormat, 'mmm', 'mm', [rfReplaceAll, rfIgnoreCase]);
      AFormat := StringReplace(Trim(AFormat), ' ', FormatSettings.DateSeparator, [rfReplaceAll]);
      AFormat := Trim(AFormat);
    end;
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
     
    procedure TZooXXXVCLMainForm.Button1Click(Sender: TObject);
    const
      _FORMATS: array[0..5] of string = ('dd mmm yy', 'dd/mm/yy', 'dddd dd mmmm e', 'dd/mm', 'ddd dd mmm', 'yyyy mmmm dddd dd');
    var
      S, Format, FormatS: String;
      lDate : TDate;
      lFormatSettings : TFormatSettings;
      I: Integer;
    begin
      for I := Low(_FORMATS)to High(_FORMATS) do
      begin
         Format := _FORMATS[I];
         S := FormatDateTime(Format, Date);
         MemoPing.Lines.Add(Format + ' : ' + S);
     
         FormatS := Format;
         S := SimplifyFormatedDate(S, FormatS);
         MemoPing.Lines.Add(Format + ' -> ' + FormatS + ' : ' + S);
     
         try
           lFormatSettings := TFormatSettings.Create();
           lFormatSettings.ShortDateFormat := FormatS[1]; // 'dd mm yyyy';
           // lFormatSettings.DateSeparator := ' '; n'a aucun effet car les espaces sont ignorész par ScanDate\ScanNumber\ScanChar
           lDate := StrToDate(S, lFormatSettings);
           MemoPing.Lines.Add(Format + ' -> ' + FormatS + ' : ' + DateToStr(lDate)) + ' & ' + FormatDateTime(Format, lDate));
         except
           on E: Exception do
           MemoPing.Lines.Add(Format + ' -> ' + FormatS + ' : ' + E.Message);
         end;
      end;
    end;
    Code result : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
     
    MemoPing
    dd mmm yy : 02 juil. 18
    dd mmm yy -> dd/mm/yy : 02/07/18
    dd mmm yy -> dd/mm/yy : 02/07/2018 & 02 juil. 18
    dd/mm/yy : 02/07/18
    dd/mm/yy -> dd/mm/yy : 02/07/18
    dd/mm/yy -> dd/mm/yy : 02/07/2018 & 02/07/18
    dddd dd mmmm e : lundi 02 juillet 18
    dddd dd mmmm e -> dd/mm/e : 02/07/18
    dddd dd mmmm e -> dd/mm/e : 02/07/2018 & lundi 02 juillet 18
    dd/mm : 02/07
    dd/mm -> dd/mm : 02/07
    dd/mm -> dd/mm : 02/07/2018 & 02/07
    ddd dd mmm : lun. 02 juil.
    ddd dd mmm -> dd/mm : 02/07
    ddd dd mmm -> dd/mm : 02/07/2018 & lun. 02 juil.
    yyyy mmmm dddd dd : 2018 juillet lundi 02
    yyyy mmmm dddd dd -> yyyy/mm/dd : 2018/07/02
    yyyy mmmm dddd dd -> yyyy/mm/dd : 02/07/2018 & 2018 juillet lundi 02
    Aide via F1 - FAQ - Guide du développeur Delphi devant un problème - Pensez-y !
    Attention Troll Méchant !
    "Quand un homme a faim, mieux vaut lui apprendre à pêcher que de lui donner un poisson" Confucius
    Mieux vaut se taire et paraître idiot, Que l'ouvrir et de le confirmer !
    L'ignorance n'excuse pas la médiocrité !

    L'expérience, c'est le nom que chacun donne à ses erreurs. (Oscar Wilde)
    Il faut avoir le courage de se tromper et d'apprendre de ses erreurs

  3. #3
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 036
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 036
    Points : 40 942
    Points
    40 942
    Billets dans le blog
    62
    Par défaut
    RE,
    Citation Envoyé par ShaiLeTroll Voir le message
    C'est con mais l'espace ne pourra jamais être un séparateur, tout espace est retiré durant l'analyse ce qui rend le découpage impossible
    C'était donc ça ! merci de cette précision, je n'avais pas assez fouillé
    MVP Embarcadero
    Delphi installés : D3,D7,D2010,XE4,XE7,D10 (Rio, Sidney), D11 (Alexandria), D12 (Athènes)
    SGBD : Firebird 2.5, 3, SQLite
    générateurs États : FastReport, Rave, QuickReport
    OS : Window Vista, Windows 10, Windows 11, Ubuntu, Androïd

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Dates] Format de date MySQL vers français
    Par tcompagnon dans le forum Langage
    Réponses: 4
    Dernier message: 17/07/2007, 13h02
  2. [Date]Format de date
    Par Nosaj dans le forum Langage SQL
    Réponses: 5
    Dernier message: 21/12/2005, 11h20
  3. conversion date timestamp vers date mysql
    Par seb0026 dans le forum SQL Procédural
    Réponses: 2
    Dernier message: 13/10/2005, 19h48

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo