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

Mailing lists / lazarus / 2012-05
8 messages · 6 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
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
FormatDateTime('YYYY-MM-DD', Now);2012/5/26 Doug Edmunds
William Oliveira Ferreira wroteFormatDateTime('YYYY-MM-DD', Now);
How does that work with a string as input?
How do I convert a string in the format yyyy-mm-dd to a DateTime variable?…
writeln(StrToDateTime('2012-05-26'));-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20120527/f5415451/attachment.html>
How do I convert a string in the format yyyy-mm-dd to a DateTime variable?…
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
How does that work with a string as input?
sorry, I understood that you wanted the opposite
2012/5/26 Doug Edmunds
How do I convert a string in the format yyyy-mm-dd to a DateTime variable?…
2012/5/27 Doug Edmunds :
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;The second parameter is only needed when '2012-05-26' is not in your locale date format.…
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.
}