Recent

Author Topic: Help to convert string to date and time?  (Read 10134 times)

elidorio

  • Sr. Member
  • ****
  • Posts: 295
Help to convert string to date and time?
« on: October 30, 2014, 08:03:30 pm »
Hello Guys,

I need some help, I need to convert a String to a date and time.
The String I'm getting this in this format: 2014/10/30 13:53:12

--
Edson
Lazarus 1.4.4 | FPC 2.6.4 | Windows / Linux Debian

dogriz

  • Jr. Member
  • **
  • Posts: 96
    • Tech blog - Delphi, Lazarus, Firebird, Windows, Linux, Android...
FPC 3.0.3
Lazarus 1.6.5
Debian x86_64, arm

mirce.vladimirov

  • Full Member
  • ***
  • Posts: 219
Re: Help to convert string to date and time?
« Reply #2 on: October 30, 2014, 10:12:37 pm »
Hello Guys,

I need some help, I need to convert a String to a date and time.
The String I'm getting this in this format: 2014/10/30 13:53:12

--
Edson

That should not be a problem, just type
Code: [Select]
myDateTimeVariable:=strtodatetime('2014/10/30 13:53:12');

elidorio

  • Sr. Member
  • ****
  • Posts: 295
Re: Help to convert string to date and time?
« Reply #3 on: October 31, 2014, 12:14:49 am »
Our I'm with the greatest suffering here.
the problem here, when converting the mydate: = StrToDateTime (GetCompiledDate);
Follow the example
Lazarus 1.4.4 | FPC 2.6.4 | Windows / Linux Debian

rvk

  • Hero Member
  • *****
  • Posts: 3664
Re: Help to convert string to date and time?
« Reply #4 on: October 31, 2014, 10:53:13 am »
If your GetCompiledDate isn't in the correct format (i.e. according to your own language/region settings) you should give the correct format to the StrToDateTime function.

So you could do this (because "{$I %DATE%} {$I %TIME%}" is in format "yyyy/mm/dd hh:mm:ss") :
Code: [Select]
var
  FS: TFormatSettings;
begin
  FS := DefaultFormatSettings;
  FS.DateSeparator := '/';
  FS.ShortDateFormat := 'yyyy/mm/dd';
  FS.ShortTimeFormat := 'hh:mm:ss';
  mydate := StrToDateTime(GetCompiledDate, FS);

Easiest is to create a separate function for this (just as you did with GetCompiledDate).
Code: [Select]
function GetCompiledDateTime: TDateTime;
var
  FS: TFormatSettings;
begin
  FS := DefaultFormatSettings;
  FS.DateSeparator := '/';
  FS.ShortDateFormat := 'yyyy/mm/dd';
  FS.ShortTimeFormat := 'hh:mm:ss';
  Result := StrToDateTime(GetCompiledDate, FS);
end;

elidorio

  • Sr. Member
  • ****
  • Posts: 295
Re: Help to convert string to date and time?
« Reply #5 on: October 31, 2014, 02:36:10 pm »
Hello rvk,

You're absolutely right!
Thank you very much, it worked correctly
Lazarus 1.4.4 | FPC 2.6.4 | Windows / Linux Debian

Jkey

  • New member
  • *
  • Posts: 44
Re: Help to convert string to date and time?
« Reply #6 on: October 31, 2014, 08:24:35 pm »
You can avoid changing shortdateformat, dateseparators, etc. if you use ScanDateTime function which can be found in Dateutils unit.
Code: [Select]
uses DateUtils, ...
var MyDateTime: TDateTime;
MyDateTime := ScanDateTime('YYYY/MM/DD hh:nn:ss', '2014/10/30 13:53:12');
I find it shorter, as it's a single line, and it ignores regional preferences of the user or system regional default.

elidorio

  • Sr. Member
  • ****
  • Posts: 295
Re: Help to convert string to date and time?
« Reply #7 on: November 03, 2014, 08:27:21 pm »
Hello Jkey,

In this example as below, it's not displaying the hour nor the minute and second

mydatetime:= ScanDateTime('YYYY/MM/DD hh:nn:ss', GetCompiledDate); 


[]'s

Edson
Lazarus 1.4.4 | FPC 2.6.4 | Windows / Linux Debian

howardpc

  • Hero Member
  • *****
  • Posts: 2985
Re: Help to convert string to date and time?
« Reply #8 on: November 03, 2014, 08:57:20 pm »
try replacing
Code: [Select]
hh:nn:ss
with
Code: [Select]
hh:mm:ss

Jkey

  • New member
  • *
  • Posts: 44
Re: Help to convert string to date and time?
« Reply #9 on: November 03, 2014, 10:20:12 pm »
I'm sorry, '/' char in ScanDateTime is replaced by the default DateSeparator character which may be different on your computer. Then you can change the DateSeparator char like the previous posts showed, or you can ignore these characters like this:
Code: [Select]
function GetCompileDateTime: TdateTime;
begin
  Result := ScanDateTime('YYYYMMDD hhnnss', DelChars(DelChars(GetCompiledDate, '/'), ':'));
end;
This would need StrUtils unit...

rvk

  • Hero Member
  • *****
  • Posts: 3664
Re: Help to convert string to date and time?
« Reply #10 on: November 03, 2014, 10:46:15 pm »
You can avoid changing shortdateformat, dateseparators, etc.
Then you can change the DateSeparator char like the previous posts showed...
I do agree that ScanDateTime is a cleaner solution but please note that the solution I provided with StrToDateTime and TFormatSettings does not change the default system settings for shortdateformat, DateSeparator etc. It only takes these values as default into a separate local record and changes only that record to give to the StrToDateTime function. (Nothing is changed on system-level)

wp

  • Hero Member
  • *****
  • Posts: 5639
Re: Help to convert string to date and time?
« Reply #11 on: November 03, 2014, 10:50:21 pm »
If you absolutely need the slashes and colons whatever the locale settings, put them in quotes, and ScanDateTime won't replace them:
Code: [Select]
mydatetime:= ScanDateTime('YYYY"/"MM"/"DD hh":"nn":"ss', GetCompiledDate);

Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6993
Re: Help to convert string to date and time?
« Reply #12 on: November 04, 2014, 04:55:13 pm »
(btw, I originally did Scandatetime because I needed to be able to configure some incoming date formats in an text/ini file)