WriteLn (DayStr, ', ' ,Day, ' ' ,MonthStr, ' ' ,Year, '.' ) end. ErrorsĮxample Program Example2 uses Dos constĭayStr : array of string = ( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ) Smart Pascal : FormatDateTime command 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 (. mday is the day of the month, wday is the day of the week, starting with Sunday as day 0. (There are much better ways of writing it.GetDate 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 function tries to read dates formatted as ''įunction read_date_format_2( s: string ): tDateTime 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. 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. Libraries tend to grow and change over time. If you have to stop threads and free objects you must find a way to do this before the library unloading - it is too late to do it here. Well, that should be enough to get you started. creating and freeing objects on the heap or even using Pascal strings inside this procedure is a sure way to call for trouble. 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.) 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.
0 Comments
Leave a Reply. |