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?
lloydie-tAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.