Converting LongTimeFormat to another LongTimeFormat.

Posted on 2004-11-11
Last Modified: 2010-04-05

    I need a function that would convert any LongTimeFormat time to the current LongTimeFormat time.

The problem is i am reading values from some files, and when i read the time(it's written in the file ), it was never saved in the same time format, ( i know bad programming, not my fault just fixing it ), so now i need to save this elsewhere, but how can i convert all those format. I've checked the file i know that i have at least 4 different time format, and the best would be to handle all case because i don't know what where all the format used.

Question by:Chadak
    LVL 27

    Expert Comment

    if you use d7,
    then there is an overloaded version of the strtodate or strtotime or strtodatetime-functions,
    you could use

    maybe you should show your time-versions

    meikl ;-)
    LVL 1

    Author Comment

    This is the sample string that i have, maybe there is other format but i don't know.

    1:31:35 PM
    01:32:34 p.m.
    01:33:17 nm (did a bit of research on that one, seems to be the afrikaans regional setting)

    one of my concerns is, i know of those format, is there other format that could exist... is there a way to do some kind of universal function that could convert them all??

    i use D6

    LVL 14

    Accepted Solution

    This works on the 4 examples you gave above. You should easily be able to modify it to suite any other formats you might come across.

    function GetAsTime(s: string): TDateTime;
    const pmStrings : array[0..3] of string = ('PM','p.m.','nm','n.m');
          amStrings : array[0..3] of string = ('am','a.m.','vm','v.m');
      function InAfternoon(s: string):boolean;
      var i: integer;
        for i:= low(pmStrings) to High(pmStrings) do
          if s = pmStrings[i] then
            result:= true;
        //if still not found, check for amStrings
        for i:= low(amStrings) to High(amStrings) do
          if s = amStrings[i] then
            result:= false;

        //if still not found report error
        Raise exception.Create('An unknown am/pm qualifier was detected: '+s);
    var Hr, Min, Sec, p1,p2: word;
      p1:= Pos(':',s);
      Hr := StrToInt(LeftStr(s, p1-1));
      p2:= PosEx(':',s,p1+1);
      Min:= StrToInt(Copy(s,p1+1,p2-p1-1));
      Sec:= StrToInt(copy(s,p2+1,2));
      p2:= p2+2;

      //check for am/pm qualifiers
      if Hr <= 12 then //only if HR is less than 12 else we know it is in afternoon
        if Length(s) > p2 then //is there a qualifier, otherwise assume morning
          if InAfternoon(trim(RightStr(s, Length(s)-p2)))
          then Hr:= Hr+12;

      result:= EncodeTime(Hr, Min, Sec, 0);

    LVL 14

    Expert Comment

    by:Pierre Cornelius
    You're right about the Afrikaans.

    nm stands for "na middag" which directly translates to "after midday"
    vm stands for "voor middag" which directly translates to "before midday"
    LVL 1

    Author Comment

    Thx for your input Pierre!

       I had come to a solution close to yours. The only exception was that my constant that define the AM/PM qualifier where all un uppercase, and after the string compare would be "if uppercase(s) = amStrings[i] then"

    LVL 1

    Author Comment

    Also to determine if a time qualifier was used, i was looking if a 'M' existed in the string. iIt seems to be a valid rule for that check.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
    Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    This video discusses moving either the default database or any database to a new volume.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now