Home > Back-end >  Data type conversion: string into a time type
Data type conversion: string into a time type

Time:10-24

String into a time type,

Time display control using datetimepicker,
Purpose is to display the first day of a year in the datetimepicker control:
Var
Sdate: string;

Sdate=formatdatetime (yyyy, date);//take years, such as 2012
Sdate: sdate=+ '- 01-01';//take years and in the previous step - 01-01 is the first day of every year, such as the 2012-01-01
Datetimepicker1. Date:=strtodate (sdate);//the conversion of data types, converts a string type into time type

Runtime error is given: '2012-01-01' is not a valid date and time.

Is there a problem I do this? Where the problem?

In addition, a good way?

Please explain in detail, thanks.

CodePudding user response:

Supplement: same program under Windows XP and Windows 2003 are no problem, but in 2008 to run the error message,

CodePudding user response:

USES DateUtils;
StartOfTheYear

CodePudding user response:

refer to the second floor s11ss response:
USES DateUtils;
StartOfTheYear


study

CodePudding user response:

refer to the second floor s11ss response:
USES DateUtils;
StartOfTheYear



I also do this now, is to do not have a unified format:
Inserted in the database is 2012-1-1, not 2012-01-01

CodePudding user response:

Note that time is a format string, such as the 2012-12-1, 2012/12/1 etc., you should pay attention to your current separator is'/' or '-',

CodePudding user response:

The
reference 5 floor andrew57 response:
note that time is a format string, such as the 2012-12-1, 2012/12/1 etc., you should pay attention to your current separator is'/' or '-',


Ha ha, I said it's not the delimiter, but on January 1, 2012 is 2012-01-01 or 2012-01-01, ten digits with 0 is lacking,

CodePudding user response:

Sets the system time format, this transformation is generally system time format transformation in the characters of different with you, such as system time format: 2012/01/01, and now you want to turn the 2012-01-01, the system will think the time format is illegal!

CodePudding user response:

Will choose the time zone in the control panel for short date format changed to - the dd yyyy - mm, also can be directly modified with a related API in Delphi,

CodePudding user response:

Is the problem of time format, the time of the native format and your input format is not the same, just can have this hint, appraisal,

CodePudding user response:


Var
VFormatSettings: TFormatSettings;
The begin
With vFormatSettings do
The begin
CurrencyString:=';
CurrencyFormat:=0;
NegCurrFormat:=0;
ThousandSeparator:=', ';
DecimalSeparator:=';
CurrencyDecimals:=0;
DateSeparator:='-';//date separator
ShortDateFormat:='- dd yyyy - mm;
LongDateFormat:='- dd yyyy - mm;
TimeSeparator:=':';//time delimiters
TimeAMString:='am';
TimePMString:='PM';
ShortTimeFormat:='hh: mm;
LongTimeFormat:='hh: mm: ss';
ListSeparator:=', ';
end;
Result:=Sysutils StrToDateTime (S, vFormatSettings);
end;

CodePudding user response:

Or modify the system date format

CodePudding user response:

It is ok to add this in engineering documents, according to your requirements set ShortDataFormat format,
 Application. The Initialize; 

DateSeparator:='-';
ShortDateFormat:='- dd yyyy - MM;
Application. UpdateFormatSettings:=False;
  • Related