Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2012-05

StrToDateTime question

8 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Doug Edmunds

Hello,

How do I convert a string in the format
yyyy-mm-dd to a DateTime variable?

StrToDateTime('2012-05-26', ????); // what goes there?//

-dae

Reply by email

William Oliveira Ferreira
to Doug Edmunds
FormatDateTime('YYYY-MM-DD', Now);

2012/5/26 Doug Edmunds

Show quoted text 18 lines
Hello, How do I convert a string in the format yyyy-mm-dd to a DateTime variable? StrToDateTime('2012-05-26', ????); // what goes there?// -dae -- ______________________________**_________________ Lazarus mailing list .**freepascal.org http://lists.lazarus.**freepascal.org/mailman/**listinfo/lazarus<http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus>
________________________________ William de Oliveira Ferreira Bacharel em Sistemas de Informa??o -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20120526/1e364fe0/attachment.html>

Reply by email

ik
to Doug Edmunds: How do I convert a string in the format yyyy-mm-dd to a DateTime variable?…
Doug Edmunds wrote 7 lines
Hello, How do I convert a string in the format yyyy-mm-dd to a DateTime variable? StrToDateTime('2012-05-26', ????); // what goes there?//
writeln(StrToDateTime('2012-05-26'));
Show quoted text 12 lines
-dae -- ______________________________**_________________ Lazarus mailing list .**freepascal.org http://lists.lazarus.**freepascal.org/mailman/**listinfo/lazarus<http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20120527/f5415451/attachment.html>

Reply by email

Ludo Brands
to Doug Edmunds: How do I convert a string in the format yyyy-mm-dd to a DateTime variable?…
Show quoted text 5 lines
How do I convert a string in the format yyyy-mm-dd to a DateTime variable? StrToDateTime('2012-05-26', ????); // what goes there?//

The second parameter is only needed when '2012-05-26' is not in your locale date format. The way you use it is:

var
  FS:TFormatSettings;
  dt:tdatetime;
begin
  FS:=DefaultFormatSettings;
  FS.DateSeparator:='-';
  FS.ShortDateFormat:='yyyy-mm-dd';
  dt:=StrToDateTime('2012-05-26',FS);
end.

Note that specifying a ShortDateFormat of 'yyyy-mm-dd' is not enough to set the DateSeparator to '-'.

Ludo

Reply by email

William Oliveira Ferreira
to Doug Edmunds: How does that work with a string as input?

sorry, I understood that you wanted the opposite

2012/5/26 Doug Edmunds

Show quoted text 18 lines
On 5/26/2012 6:30 PM, William Oliveira Ferreira wrote: > FormatDateTime('YYYY-MM-DD', Now); > How does that work with a string as input? -- ______________________________**_________________ Lazarus mailing list .**freepascal.org http://lists.lazarus.**freepascal.org/mailman/**listinfo/lazarus<http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus>
________________________________ William de Oliveira Ferreira Bacharel em Sistemas de Informa??o -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20120527/b4c5d552/attachment.html>

Reply by email

Salvatore Coppola
to Doug Edmunds: How do I convert a string in the format yyyy-mm-dd to a DateTime variable?…

2012/5/27 Doug Edmunds :

Show quoted text 8 lines
Hello, How do I convert a string in the format yyyy-mm-dd to a DateTime variable? StrToDateTime('2012-05-26', ????); // what goes there?//

Lazarus/FPC help (F1) give:

file:///usr/share/doc/fp-docs/2.6.0/rtl/sysutils/tformatsettings.html

TFormatSettings

Record with all the strings that determine formatting

Declaration

Source position: sysinth.inc line 33

type TFormatSettings = record

  CurrencyFormat: Byte;

Currency format string

  NegCurrFormat: Byte;

Negative currency format string

  ThousandSeparator: Char;

Thousands separator character

  DecimalSeparator: Char;

Decimal separator character

  CurrencyDecimals: Byte;

Currency decimals

  DateSeparator: Char;

Date separator character

  TimeSeparator: Char;

Time separator character

  ListSeparator: Char;

List separator character

  CurrencyString: String;

Currency string

  ShortDateFormat: String;

Short date format string

  LongDateFormat: String;

Long Date Format string

  TimeAMString: String;

AM time indicator string

  TimePMString: String;

PM time indicator string

  ShortTimeFormat: String;

Short time format string

  LongTimeFormat: String;

Long time format string

  ShortMonthNames: TMonthNameArray;

Array with short month names

  LongMonthNames: TMonthNameArray;

Array with long month names

  ShortDayNames: TWeekNameArray;

Array with short day names

  LongDayNames: TWeekNameArray;

Long day names

  TwoDigitYearCenturyWindow: Word;

Value for 2 digit year century window

end;
Show quoted text 9 lines
-dae -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Marco van de Voort
to Ludo Brands: The second parameter is only needed when '2012-05-26' is not in your locale date format.…
Ludo Brands wrote 12 lines
The second parameter is only needed when '2012-05-26' is not in your locale date format. The way you use it is: var FS:TFormatSettings; dt:tdatetime; begin FS:=DefaultFormatSettings; FS.DateSeparator:='-'; FS.ShortDateFormat:='yyyy-mm-dd'; dt:=StrToDateTime('2012-05-26',FS); end.

Or use the more general case, scandatetime.

http://www.freepascal.org/docs-html/rtl/dateutils/scandatetime.html

which is essentially the inverse of formatdatetime (as far as reasonably possible.

there are some more comments about what it can and cannot do in the source:

{
Inverse of formatdatetime, destined for the dateutils unit of FPC.

Limitations/implementation details:
- An inverse of FormatDateTime is not 100% an inverse, simply because one
- can put e.g. time tokens twice in the format string, and scandatetime wouldn't know which time to pick.
- Strings like hn can't be reversed safely. E.g. 1:2 (2 minutes after 1)
- delivers 12 which is parsed as 12:00 and then
misses chars for the "n" part.
- trailing characters are ignored.
- no support for Eastern Asian formatting characters since they are
- windows only.
- no MBCS support.

Extensions
- #9 eats whitespace.
- whitespace at the end of a pattern is optional.
- ? matches any char.
- Quote the above chars to really match the char.
}

Reply by email