开发者

converting string to date, and getting difference in days

I have strings in the following format e.g. 2011开发者_如何学运维0201 and I want to convert it to a date in delphi, then subtract it from today to get the number of days difference.

How can I do that? Sorry for my english.


The functions you need are Copy (to retrieve parts of your input string), StrToInt (to convert the string parts to integers), DateUtils.EncodeDate (to create a TDate from the integers), Now (to retrieve the current date as TDate) and DateUtils.DaysBetween (to get the difference between two dates in days).

I think you can figure out the rest on your own using the Delphi help for these functions.


Use Copy to extract the 3 substrings for year, month, day. Then StrToInt to convert them into integers, then EncodeDate to turn the 3 integers into a TDateTime.

TDateTime is a float, counting whole days before the decimal point and the time at that day as the fraction after the decimal point.

In SysUtils there is a function called Date: TDateTime which returns the current date.

You can simply substract the TDateTime you parsed out of the string from the value returned by Date to get the number of days difference between them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜