[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 733
  • Last Modified:

convert date and time string to unixtime

I need to covert a date and time string to a unixtime string.
the date and time string would look something like '2004-11-18 23:27:00'

Can you help?
0
lloydie-t
Asked:
lloydie-t
  • 4
  • 3
  • 2
1 Solution
 
Drunkard_EnglishmanCommented:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function Str2UnixTime(s: string): LongWord;
var
  years, months, days, hours, minutes, seconds: Integer;
begin
  if (s[5]  <> '-') or
     (s[8]  <> '-') or
     (s[11] <> ' ') or
     (s[14] <> ':') or
     (s[17] <> ':') then raise EConvertError.Create('Invalid Date String');

  try
    years   := StrToInt(Copy(s, 1, 4)) - 1970;
    months  := StrToInt(Copy(s, 6, 2));
    days    := StrToInt(Copy(s, 9, 2));
    hours   := StrToInt(Copy(s, 12, 2));
    minutes := StrToInt(Copy(s, 15, 2));
    seconds := StrToInt(Copy(s, 18, 2));
    result := years * 31536000 + months * 2678400 + days * 86400 + hours * 3600 + minutes * 60 + seconds;
  except
    result := 0;
    raise;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
const
  s = '2004-11-18 23:27:00';
begin
caption := IntToStr(Str2UnixTime(s));
end;

end.
0
 
mokuleCommented:
or

uses DateUtils

 DateTimeToUnix(StrToDateTime(s));

in case You are happy D7 owner
0
 
Drunkard_EnglishmanCommented:
doesnt that function read dates in 'DD/MM/YYYY HH:NN:SS' format?

lloydie-t  wants to read 'YYYY-MM-DD HH:NN:SS'
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
mokuleCommented:
In this case he should specify this format in an overloaded version of StrToDateTime procedure
0
 
Drunkard_EnglishmanCommented:
my mistake the

To Quote Delphi 6 Help File:
"The S parameter must use the current locale’s date/time format. In the US, this is commonly MM/DD/YY HH:MM:SS format."

this means its linked to the locale settings, so it will differ depending on the os' language settings.

"overloaded version of StrToDateTime procedure" ?

you mean write a function to convert it to a TDateTime only to use another function to convert it to a unix time? or am i not understanding correctly?

0
 
lloydie-tAuthor Commented:
Can you let me know how to specify my format in an overloaded version of StrToDateTime procedure?
0
 
lloydie-tAuthor Commented:
No worry formatted may string DD/MM/YYYY. All works OK now.

Thanks
0
 
mokuleCommented:
var
  t: TDateTime;
  fs: TFormatSettings;
begin
  GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT,fs);
  fs.ShortDateFormat := 'YYYY-MM-DD';
  fs.LongTimeFormat :=  'HH:NN:SS';
  fs.DateSeparator := '-';
  fs.TimeSeparator := ':';
  t := StrToDateTime('2004-11-18 23:27:00',fs);
  Edit1.Text := DateTimeToStr(t);
0
 
Drunkard_EnglishmanCommented:
and there ends my confusion, they added more functionality after delphi 6
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now