delphi FormatDateTime

FormatDateTimeFunctionRichformattingofaTDateTimevariableintoastringSysUtilsunit1 function FormatDateTime(constFormatting:string;DateTime:TDateTime):stri

大家好,又见面了,我是你们的朋友全栈君。

 

 

FormatDateTime
Function
Rich formatting of a TDateTime variable into a string SysUtils unit
1  function FormatDateTime(const Formatting : string; DateTime : TDateTime) : string;
2  function FormatDateTime (const Formatting : string; DateTime : TDateTime; const FormatSettings : TFormatSettings)  : string;
 

 

Description
The FormatDateTime function provides rich formatting of a TDateTime value DateTime into a string. Formatting is defined by the Formatting string.
 
The Formatting string can comprise a mix of ordinary characters (that are passed unchanged to the result string), and data formatting characters. This formatting is best explained by the example code.
 
The following (non-Asian) formatting character strings can be used in the Formatting string:
 

= Year last 2 digits
yy  = Year last 2 digits
yyyy  = Year as 4 digits
= Month number no-leading 0
mm  = Month number as 2 digits
mmm  = Month using ShortDayNames (Jan)
mmmm  = Month using LongDayNames (January)
= Day number no-leading 0
dd  = Day number as 2 digits
ddd  = Day using ShortDayNames (Sun)
dddd  = Day using LongDayNames  (Sunday)
ddddd  = Day in ShortDateFormat
dddddd  = Day in LongDateFormat
   
= Use ShortDateFormat + LongTimeFormat
= Hour number no-leading 0
hh  = Hour number as 2 digits
n = Minute number no-leading 0
nn  = Minute number as 2 digits
= Second number no-leading 0
ss  = Second number as 2 digits
z = Milli-sec number no-leading 0s
zzz  = Milli-sec number as 3 digits
= Use ShortTimeFormat
tt  = Use LongTimeFormat
   
am/pm  = Use after h : gives 12 hours + am/pm
a/p  = Use after h : gives 12 hours + a/p
ampm  = As a/p but TimeAMString,TimePMString
= Substituted by DateSeparator value
: = Substituted by TimeSeparator value

 
Important : if you want to see characters such as dd in the formatted output, placing them in ” marks will stop them being interpreted as date or time elements.
 
In addition to this formatting, various of the above options are affected by the following variables, withe their default values :
 

DateSeparator  = /
TimeSeparator  = :
ShortDateFormat  = dd/mm/yyyy
LongDateFormat  = dd mmm yyyy
TimeAMString  = AM
TimePMString  = PM
ShortTimeFormat  = hh:mm
LongTimeFormat  = hh:mm:ss
ShortMonthNames  = Jan Feb …
LongMonthNames  = January, February …
ShortDayNames  = Sun, Mon …
LongDayNames  = Sunday, Monday …
TwoDigitYearCenturyWindow  = 50

 
Version 2 of this function is for use within threads. You furnish the FormatSettings record before invoking the call. It takes a local copy of global formatting variables that make the routine thread safe.

 
Related commands
DateSeparator The character used to separate display date fields
DateTimeToStr Converts TDateTime date and time values to a string
DateTimeToString Rich formatting of a TDateTime variable into a string
LongDateFormat Long version of the date to string format
LongDayNames An array of days of the week names, starting 1 = Sunday
LongMonthNames An array of days of the month names, starting 1 = January
LongTimeFormat Long version of the time to string format
ShortDateFormat Compact version of the date to string format
ShortDayNames An array of days of the week names, starting 1 = Sunday
ShortMonthNames An array of days of the month names, starting 1 = Jan
ShortTimeFormat Short version of the time to string format
StrToDateTime Converts a date+time string into a TDateTime value
TimeAMString Determines AM value in DateTimeToString procedure
TimePMString Determines PM value in DateTimeToString procedure
TimeSeparator The character used to separate display time fields
TwoDigitYearCenturyWindow Sets the century threshold for 2 digit year string conversions
 
 
Example code : Showing all of the date field formatting data types
var
  myDate : TDateTime;

begin

  // Set up our TDateTime variable with a full date and time :
  // 5th of June 2000 at 01:02:03.004  (.004 milli-seconds)
  myDate := EncodeDateTime(2000, 6, 5, 1, 2, 3, 4);

  // Date only – numeric values with no leading zeroes (except year)
  ShowMessage(‘              d/m/y = ‘+
              FormatDateTime(‘d/m/y’, myDate));

  // Date only – numeric values with leading zeroes
  ShowMessage(‘           dd/mm/yy = ‘+
              FormatDateTime(‘dd/mm/yy’, myDate));

  // Use short names for the day, month, and add freeform text (‘of’)
  ShowMessage(‘  ddd d of mmm yyyy = ‘+
              FormatDateTime(‘ddd d of mmm yyyy’, myDate));

  // Use long names for the day and month
  ShowMessage(‘dddd d of mmmm yyyy = ‘+
              FormatDateTime(‘dddd d of mmmm yyyy’, myDate));

  // Use the ShortDateFormat settings only
  ShowMessage(‘              ddddd = ‘+
              FormatDateTime(‘ddddd’, myDate));

  // Use the LongDateFormat settings only
  ShowMessage(‘             dddddd = ‘+
              FormatDateTime(‘dddddd’, myDate));

  // Use the ShortDateFormat + LongTimeFormat settings
  ShowMessage(‘                  c = ‘+
              FormatDateTime(‘c’, myDate));
end;
Show full unit code
                 d/m/y = 5/6/00
             dd/mm/yy = 05/06/00
    ddd d of mmm yyyy = Mon 5 of Jun 2000
  dddd d of mmmm yyyy = Monday 5 of June 2000
                ddddd = 05/06/2000
               dddddd = 05 June 2000
                    c = 05/06/2000 01:02:03
 
 
Example code : Showing all of the time field formatting data types
var
  myDate : TDateTime;

begin

  // Set up our TDateTime variable with a full date and time :
  // 5th of June 2000 at 01:02:03.004  (.004 milli-seconds)
  myDate := EncodeDateTime(2000, 6, 5, 1, 2, 3, 4);

  // Time only – numeric values with no leading zeroes
  ShowMessage(‘     h:n:s.z = ‘+FormatDateTime(‘h:n:s.z’, myDate));

  // Time only – numeric values with leading zeroes
  ShowMessage(‘hh:nn:ss.zzz = ‘+FormatDateTime(‘hh:nn:ss.zzz’, myDate));

  // Use the ShortTimeFormat settings only
  ShowMessage(‘           t = ‘+FormatDateTime(‘t’, myDate));

  // Use the LongTimeFormat settings only
  ShowMessage(‘          tt = ‘+FormatDateTime(‘tt’, myDate));

  // Use the ShortDateFormat + LongTimeFormat settings
  ShowMessage(‘           c = ‘+FormatDateTime(‘c’, myDate));
end;
Show full unit code
        h:m:s.z = 1:2:3.4
  hh:mm:ss.zzz = 01:02:03.004
             t = 01:02
            tt = 01:02:03
             c = 05/06/2000 01:02:03
 
 
Example code : Showing the effect of local date format settings
var
  myDate : TDateTime;

begin

  // Set up our TDateTime variable with a full date and time :
  // 5th of June 2049 at 01:02:03.004  (.004 milli-seconds)
  //
  // Note that 49 is treated as 2049 as follows :
  //               TwoDigitYearCenturyWindow => 50
  //                            Current year => 2008 (at time of writing)
  //      Subtract TwoDigitYearCenturyWindow => 1958
  //            2 digit year to be converted => 49
  //  Compare with the last 2 digits of 1958 => Less
  //      So the year is in the next century => 2049
  // (58 would be converted to 1958)

  myDate := StrToDateTime(’05/06/49 01:02:03.004′);

  // Demonstrate default locale settings

  // Use the DateSeparator and TimeSeparator values
  ShowMessage(‘dd/mm/yy hh:nn:ss = ‘+
              FormatDateTime(‘dd/mm/yy hh:nn:ss’, myDate));

  // Use ShortMonthNames
  ShowMessage(‘              mmm = ‘+FormatDateTime(‘mmm’, myDate));

  // Use LongMonthNames
  ShowMessage(‘             mmmm = ‘+FormatDateTime(‘mmmm’, myDate));

  // Use ShortDayNames
  ShowMessage(‘              ddd = ‘+FormatDateTime(‘ddd’, myDate));

  // Use LongDayNames
  ShowMessage(‘             dddd = ‘+FormatDateTime(‘dddd’, myDate));

  // Use the ShortDateFormat string
  ShowMessage(‘            ddddd = ‘+FormatDateTime(‘ddddd’, myDate));

  // Use the LongDateFormat string
  ShowMessage(‘           dddddd = ‘+FormatDateTime(‘dddddd’, myDate));

  // Use the TimeAmString
  ShowMessage(‘           hhampm = ‘+FormatDateTime(‘hhampm’, myDate));

  // Use the ShortTimeFormat string
  ShowMessage(‘                t = ‘+FormatDateTime(‘t’, myDate));

  // Use the LongTimeFormat string
  ShowMessage(‘               tt = ‘+FormatDateTime(‘tt’, myDate));

  // Use the TwoDigitCenturyWindow
  ShowMessage(‘       dd/mm/yyyy = ‘+
              FormatDateTime(‘dd/mm/yyyy’, myDate));

  ShowMessage(”);

  // Now change the defaults
  DateSeparator      := ‘-‘;
  TimeSeparator      := ‘_’;
  ShortDateFormat    := ‘dd/mmm/yy’;
  LongDateFormat     := ‘dddd dd of mmmm of yyyy’;
  TimeAMString       := ‘morning’;
  TimePMString       := ‘afternoon’;
  ShortTimeFormat    := ‘hh:nn:ss’;
  LongTimeFormat     := ‘hh : nn : ss . zzz’;
  ShortMonthNames[6] := ‘JUN’;
  LongMonthNames[6]  := ‘JUNE’;
  ShortDayNames[1]   := ‘SUN’;
  LongDayNames[1]    := ‘SUNDAY’;
  TwoDigitYearCenturyWindow := 75; // This means 49 is treated as 1949

  // Set up our TDateTime variable with the same value as before
  // except that we must use the new date and time separators
  // The TwoDigitYearCenturyWindow variable only takes effect here
  myDate := StrToDateTime(’09-02-49 01_02_03.004′);

  // Use the DateSeparator and TimeSeparator values
  ShowMessage(‘dd/mm/yy hh:nn:ss = ‘+
              FormatDateTime(‘dd/mm/yy hh:nn:ss’, myDate));

  // Use ShortMonthNames
  ShowMessage(‘              mmm = ‘+FormatDateTime(‘mmm’, myDate));

  // Use LongMonthNames
  ShowMessage(‘             mmmm = ‘+FormatDateTime(‘mmmm’, myDate));

  // Use ShortDayNames
  ShowMessage(‘              ddd = ‘+FormatDateTime(‘ddd’, myDate));

  // Use LongDayNames
  ShowMessage(‘             dddd = ‘+FormatDateTime(‘dddd’, myDate));

  // Use the ShortDateFormat string
  ShowMessage(‘            ddddd = ‘+FormatDateTime(‘ddddd’, myDate));

  // Use the LongDateFormat string
  ShowMessage(‘           dddddd = ‘+FormatDateTime(‘dddddd’, myDate));

  // Use the TimeAmString
  ShowMessage(‘           hhampm = ‘+FormatDateTime(‘hhampm’, myDate));

  // Use the ShortTimeFormat string
  ShowMessage(‘                t = ‘+FormatDateTime(‘t’, myDate));

  // Use the LongTimeFormat string
  ShowMessage(‘               tt = ‘+FormatDateTime(‘tt’, myDate));

  // Use the TwoDigitCenturyWindow
  ShowMessage(‘       dd/mm/yyyy = ‘+
              FormatDateTime(‘dd/mm/yyyy’, myDate));
end;
Show full unit code
   dd/mm/yy hh:mm:ss = 05/06/49 01:02:03
                mmm = Jun
               mmmm = June
                ddd = Sat
               dddd = Saturday
              ddddd = 05/06/2049
             dddddd = 05 June 2049
             hhampm = 01AM
                  t = 01:02
                 tt = 01:02:03
         dd/mm/yyyy = 05/06/2049
  
  dd/mm/yy hh:nn:ss = 05-06-49 01_02_03
                mmm = JUN
               mmmm = JUNE
                ddd = SUN
               dddd = SUNDAY
              ddddd = 05-JUN-49
             dddddd = SUNDAY 05 of JUNE of 1949
             hhampm = 01morning
                  t = 01_02_03
                 tt = 01 _ 02 _ 03 . 004
         dd/mm/yyyy = 05-06-1949
 

 

 

 

StrToDateTime Converts a date+time string into a TDateTime value
1  function StrToDateTime ( const DateTime : string ) : TDateTime;
2  function StrToDateTime ( const DateTime : string; const FormatSettings : TFormatSettings ) : TDateTime;

Code:

  myDateTime := StrToDateTime(’21/06/09 17′);

  myDateTime := StrToDateTime(’21/06/09 16:48:23′);

   String DateTime -> TDateTime myDateTime
   —————————————–  
   21/06/09 17       -> 21/06/09 17:00:00
   21/06/09 16:48:23 -> 21/06/09 16:48:23

StrToDate Converts a date string into a TDateTime value
1  function StrToDate ( const Date : string ) : TDateTime;
2  function StrToDate ( const Date : string; const FormatSettings : TFormatSettings ) : TDateTime;

Code:

  myDate := StrToDate(’18/06/09′);

  myDate := StrToDate(’18/06/09′);


StrToTime Converts a time string into a TDateTime value

1  function StrToTime ( const Time : string ) : TDateTime;
2  function StrToTime ( const Time : string; const FormatSettings : TFormatSettings ) : TDateTime;

Code:

  myTime := StrToTime(‘9PM’);

  myTime := StrToTime(’21’);

  myTime := StrToTime(’21:01′);

  myTime := StrToTime(’21:01:01′);

   String Time -> TDateTime myTime
   —————————————–  
   9PM      -> 21:00:00
   21       -> 21:00:00
   21:01    -> 21:01:00
   21:01:01 -> 21:01:01

The following function ConvertToDateTime also converts a date string in DDMMYYYY format to a TDateTime
An exception is thrown on an invalid format.

Code:

function ConvertToDateTime(const strDate : String): TDateTime;

var

  strDay, strMonth, strYear: Word;

begin

  strDay := StrToInt(Copy(strDate, 1, 2));

  strMonth := StrToInt(Copy(strDate, 3, 2));

  strYear := StrToInt(Copy(strDate, 5, 4));

  Result := EncodeDate(strYear, strMonth, strDay);

end;

function IsValidDate(const strDate : String): Boolean;

begin

  Result := True;

  try

    ConvertToDateTime(strDate);

  except

    Result := False;

  end;

end;

 

y  = Year last 2 digits
yy = Year last 2 digits
yyyy  = Year as 4 digits
m  = Month number no-leading 0
mm  = Month number as 2 digits
mmm  = Month using ShortDayNames (Jan)
mmmm  = Month using LongDayNames (January)
d  = Day number no-leading 0
dd  = Day number as 2 digits
ddd  = Day using ShortDayNames (Sun)
dddd  = Day using LongDayNames  (Sunday)
ddddd  = Day in ShortDateFormat
dddddd  = Day in LongDateFormat
  
c  = Use ShortDateFormat + LongTimeFormat
h  = Hour number no-leading 0
hh  = Hour number as 2 digits
n = Minute number no-leading 0
nn  = Minute number as 2 digits
s  = Second number no-leading 0
ss  = Second number as 2 digits
z = Milli-sec number no-leading 0s
zzz  = Milli-sec number as 3 digits
t  = Use ShortTimeFormat
tt  = Use LongTimeFormat
  
am/pm  = Use after h : gives 12 hours + am/pm
a/p  = Use after h : gives 12 hours + a/p
ampm  = As a/p but TimeAMString,TimePMString
/  = Substituted by DateSeparator value
: = Substituted by TimeSeparator value

DateSeparator  = /
TimeSeparator  = :
ShortDateFormat  = dd/mm/yyyy
LongDateFormat  = dd mmm yyyy
TimeAMString  = AM
TimePMString  = PM
ShortTimeFormat  = hh:mm
LongTimeFormat  = hh:mm:ss
ShortMonthNames  = Jan Feb …
LongMonthNames  = January, February …
ShortDayNames  = Sun, Mon …
LongDayNames  = Sunday, Monday …
TwoDigitYearCenturyWindow  = 50

The default format in Australia is day/month/year, eg. ’21/6/2009′.
MyDateTime : TDateTime = StrToDate(’21/6/2009′);
To change the date format from ‘6-21-2009′ to ’21/6/2009’:

Code:

strInstallationDate := ‘6-21-2009’;         

DateSeparator := ‘-‘;

          ShortDateFormat := ‘d-m-yyyy’;

          try

            MyDateTime := StrToDate(strInstallationDate);

            result := InstallationDate;

          except

            bValidDate := false;

          end;
 
 

转自:http://www.delphibasics.co.uk/RTL.asp?Name=FormatDateTime

           http://bettereducation.com.au/it/yaf_postst448_Converts-a-date-or-time-or-datetime-string-into-a-TDateTime-value-in-Delphi.aspx

 

 

 

 

 

 

 

 

 

 

 

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/125792.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python中导入numpy为什么错误_pycharm安装配置教程

    python中导入numpy为什么错误_pycharm安装配置教程今天网上复制了一个代码,其中有个importnumpyasnp,运行时提示需要安装numpy库,然后我按照网上的方法,按顺序点击File–>Settings–>Project:pythonProject–>PythonInterpreter,然后找到+那里准备添加库,如下:然后就报erroroccurredwheninstallingpackage”numpy”的错误,搞了半天都没搞定,遂找了一个经……

    2022年8月29日
    2
  • 大数据最佳实践-基于Spark的ETL开发

    大数据最佳实践-基于Spark的ETL开发目录数据同步RDMBStoRDMBS数据同步HivetoHive数据同步RDBMStoHive数据同步hivetordmbsHDFS数据监控数据同步filetohbase数据同步RDMBStoRDMBSpackagecom.sutpc.bigdata.syncimportjava.util.Propertiesimportorg.apache.log4j.{Level,Logger}importorg.apache.spark.sql.Spark

    2022年5月27日
    39
  • 本以为java语言很难学,其实就学完下面这些知识,就能理解了

    本以为java语言很难学,其实就学完下面这些知识,就能理解了刚毕业,找工作,很多人都面临相同的问题。自己能做什么?什么工作既舒服,福利又好(不存在的,除非银行你家开的)。然后社会是个发展的社会,现代人的生活越来越智能,生活中其实充满“技术”!!!所以,在各个岗位中,其实编程类的岗位工资是平均水平最高的。可以加你Java资料分享群java《学习》+交流523401738作为5大编程语言的JAVA是当今最受各大公司的青睐,很多项目,很多工程都需要用到java…

    2022年7月8日
    17
  • 数据库分库分表解决方案汇总

    点击上方“全栈程序员社区”,星标公众号 重磅干货,第一时间送达 作者 | butterfly100 来源 | cnblogs.com/butterfly100/…

    2021年6月24日
    78
  • 社会治理大数据平台怎么建_平度市社会治理大数据平台建设有序推进

    社会治理大数据平台怎么建_平度市社会治理大数据平台建设有序推进6月1日,市委常委、政法委书记陈勇调度了全市社会治理大数据平台建设进展情况。陈勇首先实地查看了市级社会治理指挥中心建设情况,详细询问了施工人员工程进展、需要协调解决的问题和困难。下午,陈勇听取了青岛城市大数据运营有限公司关于社会治理大数据平台建设推进情况汇报,指出要按照“全省最优、全国一流”的目标,加快智慧城市创新软件版块的开发和基础数据的导入,同步做好信息安全保障工作,强化实战应用,确保7月1日…

    2022年6月1日
    39
  • 自学java心路历程(学了半年。。。直到更久。。。。。)[通俗易懂]

    自学java心路历程(学了半年。。。直到更久。。。。。)[通俗易懂]自学背景环境:我是91年的。之前在小贷行业,混了快四年,经历过3家公司倒闭,在找工作发现没什么特技,太难。毕业都近4年了。但是我觉得必须要有所改变,要学。然后听了朋友的意见,不去培训学校自己自学。自学过程:自学的是java,18年三月份的时候因为大学学过2级C语言。java基础部分是在网上找到,大概都能看懂进去,一天看个7,8个小时都OK,但是比较少敲代码,主要学的是javase。java基…

    2022年6月13日
    25

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号