![]() ![]() (There are much better ways of writing it. This code is for clarity and illustration only. Result := read_date_format_3( users_input ) Result := read_date_format_2( users_input ) Result := read_date_format_1( users_input ) this function tries to read dates formatted as '2007 12 31'įunction read_date_format_3( s: string ): tDateTime įunction read_date( users_input: string ): tDateTime This formatting is best explained by the example code. The Formatting string can comprise a mix of ordinary characters (that are passed unchanged to the result string), and data formatting characters. Formatting is defined by the Formatting string. this function tries to read dates formatted as ''įunction read_date_format_2( s: string ): tDateTime The FormatDateTime function provides rich formatting of a TDateTime value DateTime into a string. For example: function read_date_format_1( s: string ): tDateTime If you know your input will take one of several distinct forms, you can use try.except blocks until you get a correct date. The way you handled it is brilliant and instructive!įinally, I would like to add some thoughts to reading time/date information from a string. that is a problem common to all digital computers. For Delphi 5 and earlier all the date stuff is in the System unit.īTW, Micheus, thanks for that nifty function! That floating point error there is not Delphi's fault though. The DateUtils unit was introduced in Delphi 6. DateTimeToString returns in Result a string representation of DateTime using the formatting specified in FormatStr. There is a lot of useful stuff in there.Īlso, the unit(s) you need to put in your uses clause vary between Delphi versions. Even if you, like he, already know quite a lot about handling dates in Delphi. Questions and comments welcome.Īs always, readers should do as Micheus did and immediately refer themselves to the Delphi documentation. Well, that should be enough to get you started. If you need to read dates more robustly, you will have to write your own routine to get all the information, then assemble it with the EncodeDate and EncodeTime functions. The caveat is, of course, that the input must be very strictly formatted. I think you need to get a better calendar.' ) Then writeln( 'Good job! You got it right!' )Įlse writeln( 'Hmm. Writeln( 'You did not enter a date like I asked you.' ) ![]() Users_date := StrToDate( users_dateString ) Write( 'Please enter the date as d-m-y: ' ) Get the user's idea of what the date is For that you will need to use the TTimeStamp type, which is a record defined as: type Sometimes you want just a little more accuracy when dealing with time than a fractional part of a day. See "Getting and Setting the hour." below for ways to access individual parts of a date, and an example. To add or subtract weeks or years, first calculate the correct number of days. You can add or subtract months with the IncMonth function: var two_months_ago: tDateTime (tomorrow is 1 day) plus (one week is 7 days) You can add or subtract days from the date. The_day_and_time := now // same as the_date + the_time So, to get the current date and time, use one of the following functions: var (Delphi 1.0 calculated the date from the year 1, so to convert a Delphi 1.0 date to a Delphi 2.0 date, use date_d2 := date_d1 -693594.0 The date format was changed to be compatible with OLE 2.0 Automation.) See alsoĬonvert a date string to a TDateTime value.Ĭonvert a time string to a TDateTime value.Ĭonverts a TDateTime value to a string using a predefined format.The question frequently comes up on how to manipulate dates and time using Delphi.ĭelphi 2.0 and later supply the TDateTime format, which is actually a floating point number (stored as a IEEE double) containing the number of days that have passed since 12 December 1899. an invalid date or invalid character), an EConvertError exception is raised. The order of the 3 numbers (y/m/d, m/d/y, d/m/y) is determined from the ShortDateFormat variable. From what I've read of StrToDateTime () and TFormatSettings, I don't see a way to do this directly. If only one number is given, it is supposed to represent the day of the current month. Using FreePascal (FPC) 2.6.2 I'd like to convert into DateTime values some timestamp strings from email messages (retrieved from an IMAP server using Synapse IMAPsend): Date: Thu, 20:07:43 -0400. If two numbers are given, they are supposed to form the day and month of the current year. The date and time parts must be separated by a space.įor the date part, the same restrictions apply as for the StrToDate function: The Date must consist of 1 to three numbers, separated by the DateSeparator character. StrToDateTime converts the string S to a TDateTime date and time value. StrToDateTime Ĭonvert a date/time string to a TDateTime value. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |