JDS2018 1 Posted December 7, 2020 Hi Can Someone help me on below i need to make app count down for 1 Year Let Say if i set Date for 2021 Dec 31 need to see to the Set Date How Many Year , months , Days , Hours , Mints, Seconds Left Share this post Link to post
aehimself 396 Posted December 7, 2020 (edited) If DateInFuture > Now Then seconds := SecondsBetween(Now, DateInFuture) else // Date already passed Then you can divide seconds down to years, months, etc. Alternatively, you can use YearsBetween, MonthsBetween, etc. Edited December 7, 2020 by aehimself Share this post Link to post
David Heffernan 2345 Posted December 7, 2020 https://stackoverflow.com/questions/65186802/countdown-timer-in-delphi Please don't cross post, or if you do, refer to the preferred version. Share this post Link to post
Guest Posted December 8, 2020 (edited) 5 hours ago, David Heffernan said: https://stackoverflow.com/questions/65186802/countdown-timer-in-delphi Please don't cross post, or if you do, refer to the preferred version. NOT FOUND! 404 Quote This question was removed from Stack Overflow for reasons of moderation. Please refer to the help center for possible explanations why a question might be removed. Google it: "Delphi stackoverflow countdown timer" for many others Edited December 8, 2020 by Guest Share this post Link to post
Bill Meyer 337 Posted December 8, 2020 6 hours ago, JDS2018 said: Hi Can Someone help me on below i need to make app count down for 1 Year Let Say if i set Date for 2021 Dec 31 need to see to the Set Date How Many Year , months , Days , Hours , Mints, Seconds Left You say you want a count down, but then you ask for an interval. Which is it that you need? Share this post Link to post
FPiette 382 Posted December 8, 2020 @JDS2018 It is time now, you the original poster, to give explanations about what you need to do. This looks like a home work you have been assigned to do. If this is the case, this would be bad that we write the code for you. You would learn much more if you try by yourself and ask small questions, one at a time, about what you cannot do. You have to prove you have done some research by yourself. And if you did and you can't still find, I'm sure someone here will help you. If you are a developer and faced with a real world problem such as a way to protect some software and make it stop working after one year, then explain that. If this something else, explain what is it. In any case, you have to prove you have done some research by yourself. And if you did and you can't still find, I'm sure someone here will help you. Understood? 1 Share this post Link to post
mvanrijnen 123 Posted December 8, 2020 (edited) Take a look to TTimeSpan in the, System.TimeSpan unit, maybe you can do something with that. For the years and months you can use YearsBetween/Monthsbetween. (also for the rest : ) Edited December 8, 2020 by mvanrijnen Share this post Link to post
A.M. Hoornweg 144 Posted December 9, 2020 On 12/7/2020 at 6:48 PM, JDS2018 said: Let Say if i set Date for 2021 Dec 31 need to see to the Set Date How Many Year , months , Days , Hours , Mints, Seconds Left There are several pitfalls here. The most important one: months don't have a constant length. For example, assume it is February 14. How many months and days is it until March 13? And until March 15? Share this post Link to post
Guest Posted December 10, 2020 (edited) implementation {$R *.dfm} // // Date and Time Support on your HELP SYSTEM! // // ALL DATE AND TIME IS STORED INTERNALLY USING A DOUBLE VALUE // THEN YOU CAN CALCULATE IT AS ANY VALUE NUMERIC! // // DONT FORGET OF "Date Time" RANGE, ACCEPTED BY DELPHI, OF COURSE! // uses DateUtils; procedure TForm1.Button1Click(Sender: TObject); var lDateTimeStart : TDateTime; lDateTimeEnd : TDateTime; lDateTimeEncoded: TDateTime; // // ... hh, mm, ss, zzz = all in milliseconds values! Use "word" type, because this value goes to 0..65535 lYears : word; lMonths : word; lDays : word; lHours : word; lMinutes : word; lSeconds : word; lMilliSec: word; // lDaysDouble : Double; lMilliSecsDouble: Double; begin // // StrDateTime dont use "milliseconds" try other function! // // format in Brazil! dd/mm/yyyy // // using default function in your IDE: lDateTimeStart := StrToDateTime('14/02/2020 00:00:01'); { 00:00:00.000 or dont inform to equal to '' } lDateTimeEnd := StrToDateTime('15/03/2020 00:00:00'); { 00:00:00.000 } // // see all other functions this unit for easy calculate! // // Using new funtions in DateUtils.pas unit! lDays := DateUtils.DaysBetween(lDateTimeEnd, lDateTimeStart); // dif between to dates include time lDaysDouble := DateUtils.DaySpan(lDateTimeEnd, lDateTimeStart); // Returns the number of days (including fractional days) between two specified TDateTime values. // ... etc to Months and Year ... and time values // if value have "a rest" then, this "rest is a "TIME" value! // // Try change the value of "time" above for one or two vars lDateTime... if not(lDays = lDaysDouble) then // days.hours-fraction begin lMilliSecsDouble := DateUtils.MilliSecondSpan(lDateTimeEnd, lDateTimeStart); // 0.nnnnnn // // lHours := DateUtils.HourOf(lMilliSecsDouble); ... etc! // // decoding a TDateTime value or your value in format "n.m" DecodeDateTime(lMilliSecsDouble, lYears, lMonths, lDays, lHours, lMinutes, lSeconds, lMilliSec); ShowMessage(Format('%d/%d/%d %d:%d:%d:%d', [lYears, lMonths, lDays, lHours, lMinutes, lSeconds, lMilliSec])); // // from here, you have a "AV" because "Years, Months, Days" is = 0/0/0 == Invalid Date! you need verify this! // // one way: // lDays := 01; // lMonths := 01; // lYears := 01; // lDateTimeEncoded := EncodeDateTime(lYears, lMonths, lDays, lHours, lMinutes, lSeconds, lMilliSec); // DateUtils.RecodeDateTime(lDateTimeEncoded, lYears, lMonths, lDays, lHours, lMinutes, lSeconds, lMilliSec); // // or // ShowMessage(FormatDateTime('01/01/01 hh:mm:ss.zzz', lDateTimeEncoded)); // other way... // or // ShowMessage(DateTimeToStr(lDateTimeEncoded)); // // SUMMARYING: THERE IS MANY OTHER FUNCTION IN YOUR IDE - OTHER MORE EASY THAN THESE! // READ YOU HELP! end; end; Edited December 10, 2020 by Guest Share this post Link to post
A.M. Hoornweg 144 Posted December 10, 2020 @emailx45 you're kinda missing the point. Decoding is not the issue here. My point is that a "month" does not have a constant value. So expressing a countdown as "2 months and 5 days left until..." is totally vague. If there is a distance of 29 days between two dates, that distance can be either exactly one month, one month plus a day, one month minus a day or one month minus two days, depending on where in the calendar you are. Share this post Link to post
FPiette 382 Posted December 10, 2020 10 minutes ago, A.M. Hoornweg said: So expressing a countdown as "2 months and 5 days left until..." is totally vague. I don't think so because it is not any month. The expression is related to the current date. So everybody will perfectly understand how long 2 months and 5 days from now represent. IMO better that 66 days. By the way, this discussion is totally useless an uninteresting 🙂 🙂 🙂 Share this post Link to post
Lajos Juhász 293 Posted December 10, 2020 10 minutes ago, FPiette said: So everybody will perfectly understand how long 2 months and 5 days from now represent. IMO better that 66 days. According to Delphi it's not 66 days but 67: Date: 15.02.2021 Days between: 67 program Project1; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, System.DateUtils; var X: Tdate; begin try { TODO -oUser -cConsole Main : Insert code here } x:=IncDay(IncMonth(now, 2), 5); WriteLn('Date: '+DateToStr(x)); WriteLn('Days between: '+IntToStr(DaysBetween(x, now))); ReadLn; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end. Share this post Link to post
Guest Posted December 10, 2020 (edited) 6 hours ago, A.M. Hoornweg said: My point is that a "month" does not have a constant value. So expressing a countdown as "2 months and 5 days left until..." is totally vague. I think that YOU should see the functions by IDE. your answer is in read your help first. Second, the time countdown or up cannot be different in any place of world. be 28, 29, 30 or 31 days in months. NOTE: IF your calendar is another, just see in your as convert it using the functions ready. if necessary search on GitHub exist many libs for this. as explaned just do the calculate using the sample above, read your help and 'find' the function necessary or do it "by hand" using math operators, like: +/, mod etc... of course, I dont give any final solution, just sample using some of dozen functions ready in RAD! here we say some like this: You're looking hair in egg! Edited December 10, 2020 by Guest Share this post Link to post
David Heffernan 2345 Posted December 10, 2020 6 hours ago, A.M. Hoornweg said: My point is that a "month" does not have a constant value. So expressing a countdown as "2 months and 5 days left until..." is totally vague. The months between now and a fixed date in the future do have known numbers of days. Share this post Link to post
Guest Posted December 13, 2020 (edited) my fault! Edited December 13, 2020 by Guest Share this post Link to post
Guest Posted December 13, 2020 On 12/7/2020 at 2:48 PM, JDS2018 said: Hi Can Someone help me on below i need to make app count down for 1 Year Let Say if i set Date for 2021 Dec 31 need to see to the Set Date How Many Year , months , Days , Hours , Mints, Seconds Left try my sample using DateUtils.pas functions: m Share this post Link to post
FPiette 382 Posted December 13, 2020 Quote lHours := System.DateUtils.HoursBetween(lDTFuture, lDTnow) mod 24; @emailx45Using mod defeat all the purpose. Assuming now is sunday 08:07 and future is tuesday 09:20 there are 49 hours (Minutes truncated). Your formula will give 1 hour. Same for all your calculations. 1 Share this post Link to post
Guest Posted December 13, 2020 to Hours, Minutes and Seconds, it's ok! using "mod" operator! the "countdown" works as expected! Just Years, Months and Days NOT because the many possibilities in this calculate! Share this post Link to post