Board index » delphi » How get a 'MM/DD/YYYY' format string..

How get a 'MM/DD/YYYY' format string..

How to get a 'MM/DD/YYYY' short format string suitable
for edit1.text as below, if use qry.fieldbyname('OrderDate').value

edit1.text:=qry.fieldbyname('OrderDate').value;

--
 [m [1;32m? ?:?Ei??aoo? bbs.yeskw.com?E[FROM: dns.hiperfect.com] [m

 

Re:How get a 'MM/DD/YYYY' format string..


Is using edit1.text:=FormatDateTime('dd/mm/yyyy',Date) appropriate here?

Quote
cocl <yeskw....@bbs.yeskw.com> wrote in message

news:3f15Yg$tPA@bbs.yeskw.com...
Quote
> How to get a 'MM/DD/YYYY' short format string suitable
> for edit1.text as below, if use qry.fieldbyname('OrderDate').value

> edit1.text:=qry.fieldbyname('OrderDate').value;

> --
>  [m [1;32m? ?:?Ei??aoo? bbs.yeskw.com?E[FROM: dns.hiperfect.com] [m

Re:How get a 'MM/DD/YYYY' format string..


?i |b bk. aoj@: ?j
: Is using edit1.text:=FormatDateTime('dd/mm/yyyy',Date) appropriate here?

This code is make a call to the function Getellipsis.GetValue
and return a vaule of type variant, I don't know how to get a
'MM/DD/YYYY' date format string:
---------------------------------------------------------
shortdateformat:='mm/dd/yyyy';
edtDate.text:=Getellipsis.GetValue('orderdate');

the return value is 'YYYY/MM/DD', why?

below is my code:
=============================================================
procedure Tp73.BtnQryClick(Sender: TObject);
begin
  shortdateformat:='mm/dd/yyyy';
  try
  if not Assigned(Getellipsis) then
     Getellipsis:=tGetellipsis.create(application);
  Getellipsis.caption:='Book agent';
  if Getellipsis.showmodal then
  begin
     edtagent.text:=Getellipsis.GetValue('agnt');       //string
     edtbook.text:=Getellipsis.GetValue('book');        //string
     edtDate.text:=Getellipsis.GetValue('orderdate');   //datetime    
  end;
  finally
    Getellipsis.free;
    Getellipsis:=nil;
  end;  
end;

function Tgetellipsis.GetValue(Fname:string):variant;
begin
  with myQry do
  begin
  //  if fieldbyname(Fname).value.fieldtype=ftDateTime then
  //     Result:=Fieldbyname(Fname).asDateTime
  //  else
       Result:=FieldByName(Fname).value;
  end;
end;

Quote
: cocl <yeskw....@bbs.yeskw.com> wrote in message

: news:3f15Yg$tPA@bbs.yeskw.com...
: > How to get a 'MM/DD/YYYY' short format string suitable
: > for edit1.text as below, if use qry.fieldbyname('OrderDate').value
: > edit1.text:=qry.fieldbyname('OrderDate').value;

--
 [m [1;36m? ?:?Ei??aoo? bbs.yeskw.com?E[FROM: dns.hiperfect.com] [m

Re:How get a 'MM/DD/YYYY' format string..


I've been wondering if anyone knows how to go the other direction, that
is
to make a function that looks something like:
function UnFormatDateTime(format : string; source : string):TdateTime;
I know about VarToDateTime, but it isn't really as powerful as this
would be.
there are of course ambiguities, such as what if source is the string
'monday', but I'm sure that that could be overcome, maybe by finding the
monday closest to no, or the next monday after now, or either of those
with 0 instead of now. In any case even with a couple of extra ambiguity
clarifiers I think such a function would vastly simplify the handling of
datetimes especially when interacting with users.
Does anyone have thoughts about this?
        -Thad
Quote
Billy KNine wrote:

> Is using edit1.text:=FormatDateTime('dd/mm/yyyy',Date) appropriate here?

> cocl <yeskw....@bbs.yeskw.com> wrote in message
> news:3f15Yg$tPA@bbs.yeskw.com...
> > How to get a 'MM/DD/YYYY' short format string suitable
> > for edit1.text as below, if use qry.fieldbyname('OrderDate').value

> > edit1.text:=qry.fieldbyname('OrderDate').value;

> > --
> >  [m [1;32m? ?:?Ei??aoo? bbs.yeskw.com?E[FROM: dns.hiperfect.com] [m

Re:How get a 'MM/DD/YYYY' format string..


"Thad Letnes" <tlet...@wizdom.com> skrev i melding
news:3A8DB22A.A6B82A0D@wizdom.com...

Quote
> I've been wondering if anyone knows how to go the other direction, that
> is
> to make a function that looks something like:
> function UnFormatDateTime(format : string; source : string):TdateTime;
> I know about VarToDateTime, but it isn't really as powerful as this
> would be.
> there are of course ambiguities, such as what if source is the string
> 'monday', but I'm sure that that could be overcome, maybe by finding the
> monday closest to no, or the next monday after now, or either of those
> with 0 instead of now. In any case even with a couple of extra ambiguity
> clarifiers I think such a function would vastly simplify the handling of
> datetimes especially when interacting with users.
> Does anyone have thoughts about this?

Hi !

While this function is not the prettiest I've written, it works (in Norway,
that is...). It's a bit ugly, because I've added functionality without going
rewriting the whole thing. It handles the following:

"dd" = today
"mo" = monday this week
"mo+" = monday next week
"mo--" = monday two weeks ago
"1"n = 1st of this month
"101" = January 1.
"1.1" = January 1.
"Jan 1",
"1. Jan"
+ the complete date formats like "1.1.2001", "1/1/2001", "1.jan 01"

...all written to handle the DDMMYY order of operators and Norwegian +
English day/month names. May not directly suit your needs, but I believe it's
quite a good indication of what may actually be done with a date input
string...

Good luck !

------
interface
  function LeadingZeros(const Text: string; Count: integer): string;
overload;
  function LeadingZeros(Number, Count: integer): string; overload;
  function StrToDateEx(S: string; DefaultDate: TDateTime): TDateTime;

implementation

function StraightenDate(S: string): string;
var
  NumDigits              : byte;
  i                      : integer;
begin
  if Length(S) = 0 then exit;
  NumDigits := 0;
  result := '';
  for i := 1 to Length(S) do
  begin
    case S[i] of
      '0'..'9':
        begin
          inc(NumDigits);
          result := result + S[i];
        end;
      '.', ':', '-':
        begin
          if NumDigits = 1 then
            insert('0', result, Length(result));
          NumDigits := 0;
        end;
      ';': exit;
    end;
  end;
  if Odd(NumDigits) then
    insert('0', result, 1); file://Length(result));
end;

function LeadingZeros(const Text: string; Count: integer): string;
begin
  result := text;
  while Length(result) < Count do
    result := '0' + result;
end;

function LeadingZeros(Number, Count: integer): string;
begin
  result := LeadingZeros(inttostr(Number), Count);
end;

function NamesToMonths(const S: string): string;
var
  StartPos               : integer;
  EndPos                 : integer;
  Tmp                    : string;

// This replaces the month name with the corresponding
// month number, if found
  function TestRemove(const Str: string; AMonth: Word): boolean;
  begin
    StartPos := Pos(Str, Tmp);
    result := StartPos > 0;
    if result then
    begin
      EndPos := StartPos + 1;
      while (Tmp[EndPos] in ['a'..'z']) and (EndPos < Length(Tmp)) do
        inc(EndPos);
      Delete(Tmp, StartPos, EndPos - StartPos);
      Insert(LeadingZeros(AMonth, 2), Tmp, StartPos);
      NamesToMonths := Tmp;
    end;
  end;

begin
  result := '';
  Tmp := LowerCase(S);
  if TestRemove('jan', 1) then exit;
  if TestRemove('feb', 2) then exit;
  if TestRemove('mar', 3) then exit;
  if TestRemove('apr', 4) then exit;
  if TestRemove('mai', 5) then exit;
  if TestRemove('may', 5) then exit;
  if TestRemove('jun', 6) then exit;
  if TestRemove('jul', 7) then exit;
  if TestRemove('aug', 8) then exit;
  if TestRemove('sep', 9) then exit;
  if TestRemove('oct', 10) then exit;
  if TestRemove('okt', 10) then exit;
  if TestRemove('nov', 11) then exit;
  if TestRemove('des', 12) then exit;
  if TestRemove('dec', 12) then exit;
  result := Tmp;
end;

function NamesToDays(const S: string): word;
begin
  if Pos('ma', S) > 0 then
    result := 2
  else if Pos('ti', S) > 0 then
    result := 3
  else if pos('to', S) > 0 then
    result := 5
  else if pos('on', S) > 0 then
    result := 4
  else if pos('fr', S) > 0 then
    result := 6
  else if pos('l?', S) > 0 then
    result := 7
  else if pos('s?', S) > 0 then
    result := 1
  else if Pos('mo', S) > 0 then
    result := 2
  else if Pos('tu', S) > 0 then
    result := 3
  else if pos('th', S) > 0 then
    result := 5
  else if pos('we', S) > 0 then
    result := 4
  else if pos('sa', S) > 0 then
    result := 7
  else if pos('su', S) > 0 then
    result := 1
  else
    result := 0; // invalid day-string...
end;

function RemoveSeparators(const S: string): string;
const
  Numbers                = '0123456789';
var
  i                      : integer;
  NumStart,
    NumCount             : Word;
begin
  NumStart := 0;
  NumCount := 0;
  result := '';
  for i := 1 to Length(S) do
  begin
    if Pos(S[i], Numbers) > 0 then
    begin
      if NumCount = 0 then
        NumStart := i;
      inc(NumCount);
    end
    else
    begin
      if NumCount = 0 then
      else if NumCount = 1 then
        result := result + LeadingZeros(Copy(S, NumStart, 1), 2)
      else
        result := result + Copy(S, NumStart, NumCount);
      NumStart := 0;
      NumCount := 0;
    end;
  end;
  if NumStart > 0 then
    result := result + Copy(S, NumStart, NumCount);
end;

function StrToDateEx(S: string; DefaultDate: TDateTime): TDateTime; //
Utvidede datokonverteringer
var
  L                      : integer;
  YY,
    MM,
    DD                   : word;
  Tmp                    : string;
  APos,
    DOW,
    CurrDayNo            : byte;
  WeekDelta              : integer;
begin
  Tmp := NamesToMonths(S);
  S := RemoveSeparators(Tmp);
  if StrToIntDef(S, -999999) = -999999 then
  begin
    WeekDelta := 0;
    DOW := NamesToDays(Tmp);
    CurrDayNo := DayOfWeek(Date);
    if DOW = 0 then
    begin file://...error !!!
      result := Date;
      exit;
    end
    else if (DOW = 1) and (CurrDayNo > 1) then
      DOW := 8;
    repeat
      APos := Pos('+', Tmp);
      if Apos > 0 then
      begin
        inc(WeekDelta);
        Delete(Tmp, APos, 1);
      end;
    until
      APos = 0;
    if WeekDelta = 0 then
      repeat
        APos := Pos('-', Tmp);
        if Apos > 0 then
        begin
          dec(WeekDelta);
          Delete(Tmp, APos, 1);
        end;
      until
        APos = 0;
    result := Date - CurrDayNo + DOW + (Weekdelta * 7);
    exit;
  end;
  S := StraightenDate(S);
  L := length(S);
  result := DefaultDate;
  try
    case L of
      0, 1, 3, 5, 7: exit;
      2:
        begin
          DecodeDate(Date, YY, MM, DD);
          result := EncodeDate(YY, MM, StrToInt(S));
        end;
      4:
        begin
          DecodeDate(Date, YY, MM, DD);
          result := EncodeDate(YY, StrToInt(Copy(S, 3, 2)), StrToInt(Copy(S,
1, 2)));
        end;
      6:
        begin
          YY := StrToInt(Copy(S, 5, 2));
          if YY > TwoDigitYearCenturyWindow then
            YY := 1900 + YY
          else
            YY := 2000 + YY;
          result := EncodeDate(YY, StrToInt(Copy(S, 3, 2)), StrToInt(Copy(S,
1, 2)));
        end;
      8:
        begin
          result := EncodeDate(StrToInt(Copy(S, 5, 4)), StrToInt(Copy(S, 3,
2)), StrToInt(Copy(S, 1, 2)));
        end;
    end;
  except
    on E: EConvertError do
    begin
      if DefaultDate <> -1 then
        result := DefaultDate
      else
        raise EConvertError.Create(Exception(ExceptObject).Message);
    end;
  end;
end;

--
Bjoerge Saether
Consultant / Developer
http://www.itte.no
Asker, Norway
bjorgeremovet...@itte.no (remove the obvious)

Re:How get a 'MM/DD/YYYY' format string..


thanks, I'll tak a look through it, is it ok with you if I post any
revisions?(with appropriat credit to you)
Quote
"Bj?rge S?ther" wrote:

> "Thad Letnes" <tlet...@wizdom.com> skrev i melding
> news:3A8DB22A.A6B82A0D@wizdom.com...
> > I've been wondering if anyone knows how to go the other direction, that
> > is
> > to make a function that looks something like:
> > function UnFormatDateTime(format : string; source : string):TdateTime;
> > I know about VarToDateTime, but it isn't really as powerful as this
> > would be.
> > there are of course ambiguities, such as what if source is the string
> > 'monday', but I'm sure that that could be overcome, maybe by finding the
> > monday closest to no, or the next monday after now, or either of those
> > with 0 instead of now. In any case even with a couple of extra ambiguity
> > clarifiers I think such a function would vastly simplify the handling of
> > datetimes especially when interacting with users.
> > Does anyone have thoughts about this?

> Hi !

> While this function is not the prettiest I've written, it works (in Norway,
> that is...). It's a bit ugly, because I've added functionality without going
> rewriting the whole thing. It handles the following:

> "dd" = today
> "mo" = monday this week
> "mo+" = monday next week
> "mo--" = monday two weeks ago
> "1"n = 1st of this month
> "101" = January 1.
> "1.1" = January 1.
> "Jan 1",
> "1. Jan"
> + the complete date formats like "1.1.2001", "1/1/2001", "1.jan 01"

> ...all written to handle the DDMMYY order of operators and Norwegian +
> English day/month names. May not directly suit your needs, but I believe it's
> quite a good indication of what may actually be done with a date input
> string...

> Good luck !

> ------
> interface
>   function LeadingZeros(const Text: string; Count: integer): string;
> overload;
>   function LeadingZeros(Number, Count: integer): string; overload;
>   function StrToDateEx(S: string; DefaultDate: TDateTime): TDateTime;

> implementation

> function StraightenDate(S: string): string;
> var
>   NumDigits              : byte;
>   i                      : integer;
> begin
>   if Length(S) = 0 then exit;
>   NumDigits := 0;
>   result := '';
>   for i := 1 to Length(S) do
>   begin
>     case S[i] of
>       '0'..'9':
>         begin
>           inc(NumDigits);
>           result := result + S[i];
>         end;
>       '.', ':', '-':
>         begin
>           if NumDigits = 1 then
>             insert('0', result, Length(result));
>           NumDigits := 0;
>         end;
>       ';': exit;
>     end;
>   end;
>   if Odd(NumDigits) then
>     insert('0', result, 1); file://Length(result));
> end;

> function LeadingZeros(const Text: string; Count: integer): string;
> begin
>   result := text;
>   while Length(result) < Count do
>     result := '0' + result;
> end;

> function LeadingZeros(Number, Count: integer): string;
> begin
>   result := LeadingZeros(inttostr(Number), Count);
> end;

> function NamesToMonths(const S: string): string;
> var
>   StartPos               : integer;
>   EndPos                 : integer;
>   Tmp                    : string;

> // This replaces the month name with the corresponding
> // month number, if found
>   function TestRemove(const Str: string; AMonth: Word): boolean;
>   begin
>     StartPos := Pos(Str, Tmp);
>     result := StartPos > 0;
>     if result then
>     begin
>       EndPos := StartPos + 1;
>       while (Tmp[EndPos] in ['a'..'z']) and (EndPos < Length(Tmp)) do
>         inc(EndPos);
>       Delete(Tmp, StartPos, EndPos - StartPos);
>       Insert(LeadingZeros(AMonth, 2), Tmp, StartPos);
>       NamesToMonths := Tmp;
>     end;
>   end;

> begin
>   result := '';
>   Tmp := LowerCase(S);
>   if TestRemove('jan', 1) then exit;
>   if TestRemove('feb', 2) then exit;
>   if TestRemove('mar', 3) then exit;
>   if TestRemove('apr', 4) then exit;
>   if TestRemove('mai', 5) then exit;
>   if TestRemove('may', 5) then exit;
>   if TestRemove('jun', 6) then exit;
>   if TestRemove('jul', 7) then exit;
>   if TestRemove('aug', 8) then exit;
>   if TestRemove('sep', 9) then exit;
>   if TestRemove('oct', 10) then exit;
>   if TestRemove('okt', 10) then exit;
>   if TestRemove('nov', 11) then exit;
>   if TestRemove('des', 12) then exit;
>   if TestRemove('dec', 12) then exit;
>   result := Tmp;
> end;

> function NamesToDays(const S: string): word;
> begin
>   if Pos('ma', S) > 0 then
>     result := 2
>   else if Pos('ti', S) > 0 then
>     result := 3
>   else if pos('to', S) > 0 then
>     result := 5
>   else if pos('on', S) > 0 then
>     result := 4
>   else if pos('fr', S) > 0 then
>     result := 6
>   else if pos('l?', S) > 0 then
>     result := 7
>   else if pos('s?', S) > 0 then
>     result := 1
>   else if Pos('mo', S) > 0 then
>     result := 2
>   else if Pos('tu', S) > 0 then
>     result := 3
>   else if pos('th', S) > 0 then
>     result := 5
>   else if pos('we', S) > 0 then
>     result := 4
>   else if pos('sa', S) > 0 then
>     result := 7
>   else if pos('su', S) > 0 then
>     result := 1
>   else
>     result := 0; // invalid day-string...
> end;

> function RemoveSeparators(const S: string): string;
> const
>   Numbers                = '0123456789';
> var
>   i                      : integer;
>   NumStart,
>     NumCount             : Word;
> begin
>   NumStart := 0;
>   NumCount := 0;
>   result := '';
>   for i := 1 to Length(S) do
>   begin
>     if Pos(S[i], Numbers) > 0 then
>     begin
>       if NumCount = 0 then
>         NumStart := i;
>       inc(NumCount);
>     end
>     else
>     begin
>       if NumCount = 0 then
>       else if NumCount = 1 then
>         result := result + LeadingZeros(Copy(S, NumStart, 1), 2)
>       else
>         result := result + Copy(S, NumStart, NumCount);
>       NumStart := 0;
>       NumCount := 0;
>     end;
>   end;
>   if NumStart > 0 then
>     result := result + Copy(S, NumStart, NumCount);
> end;

> function StrToDateEx(S: string; DefaultDate: TDateTime): TDateTime; //
> Utvidede datokonverteringer
> var
>   L                      : integer;
>   YY,
>     MM,
>     DD                   : word;
>   Tmp                    : string;
>   APos,
>     DOW,
>     CurrDayNo            : byte;
>   WeekDelta              : integer;
> begin
>   Tmp := NamesToMonths(S);
>   S := RemoveSeparators(Tmp);
>   if StrToIntDef(S, -999999) = -999999 then
>   begin
>     WeekDelta := 0;
>     DOW := NamesToDays(Tmp);
>     CurrDayNo := DayOfWeek(Date);
>     if DOW = 0 then
>     begin file://...error !!!
>       result := Date;
>       exit;
>     end
>     else if (DOW = 1) and (CurrDayNo > 1) then
>       DOW := 8;
>     repeat
>       APos := Pos('+', Tmp);
>       if Apos > 0 then
>       begin
>         inc(WeekDelta);
>         Delete(Tmp, APos, 1);
>       end;
>     until
>       APos = 0;
>     if WeekDelta = 0 then
>       repeat
>         APos := Pos('-', Tmp);
>         if Apos > 0 then
>         begin
>           dec(WeekDelta);
>           Delete(Tmp, APos, 1);
>         end;
>       until
>         APos = 0;
>     result := Date - CurrDayNo + DOW + (Weekdelta * 7);
>     exit;
>   end;
>   S := StraightenDate(S);
>   L := length(S);
>   result := DefaultDate;
>   try
>     case L of
>       0, 1, 3, 5, 7: exit;
>       2:
>         begin
>           DecodeDate(Date, YY, MM, DD);
>           result := EncodeDate(YY, MM, StrToInt(S));
>         end;
>       4:
>         begin
>           DecodeDate(Date, YY, MM, DD);
>           result := EncodeDate(YY, StrToInt(Copy(S, 3, 2)), StrToInt(Copy(S,
> 1, 2)));
>         end;
>       6:
>         begin
>           YY := StrToInt(Copy(S, 5, 2));
>           if YY > TwoDigitYearCenturyWindow then
>             YY := 1900 + YY
>           else
>             YY := 2000 + YY;
>           result := EncodeDate(YY, StrToInt(Copy(S, 3, 2)), StrToInt(Copy(S,
> 1, 2)));
>         end;
>       8:
>         begin
>           result := EncodeDate(StrToInt(Copy(S, 5, 4)), StrToInt(Copy(S, 3,
> 2)), StrToInt(Copy(S, 1, 2)));
>         end;
>     end;
>   except
>     on E: EConvertError do
>     begin
>       if DefaultDate <> -1 then
>         result := DefaultDate
>       else
>         raise EConvertError.Create(Exception(ExceptObject).Message);
>     end;
>   end;
> end;

> --
> Bjoerge Saether
> Consultant / Developer
> http://www.itte.no
> Asker, Norway
> bjorgeremovet...@itte.no (remove the obvious)

Re:How get a 'MM/DD/YYYY' format string..


"Thad Letnes" <tlet...@wizdom.com> skrev i melding
news:3A9136F6.8CBE2B93@wizdom.com...

Quote
> thanks, I'll tak a look through it, is it ok with you if I post any
> revisions?(with appropriat credit to you)

Yes, sure.

--
Bjoerge Saether
Consultant / Developer
http://www.itte.no
Asker, Norway
bjorgeremovet...@itte.no (remove the obvious)

Other Threads