Coder Social home page Coder Social logo

tdatetimehelper's Introduction

TDateTimeHelper

A TDateTime helper record for Delphi XE3 or above.

One of the updated language features for XE3 or above are record helpers for simple types.

To learn a little bit more, I decided to make a record helper for the TDateTime type. It's a very simple record helper which pretty much calls the existing date time functions that are in the DateUtils unit. One benefit is that it allows you to assign and manipulate datetime values very easily.

Getting Started

  1. Download the DateTimeHelper unit.
  2. Add DateTimeHelper unit to your project.
  3. Add DateTimeHelper to the uses clause in the implementation section of your unit.

And that's it! You now should be able to see the additional methods on TDateTime types via CodeInsight in your Delphi XE3 IDE.

Note: The limitation of helper records is that only one helper is active/available per class type. If you have more than one helper for a class type then it is the last one defined in the uses clause that will be active.

Example

uses DateTimeHelper;

var
  T1, T2: TDateTime;

begin
  // Set date to 14th September 2012
  T1 := TDateTime.Create(2012, 9, 14);
  
  // Set date to today's date
  T1 := TDateTime.Today;
  
  // Set T2 to 1 year, 3 months and 10 days ahead of T1.
  T2 := T1.AddYears(1).AddMonths(3).AddDays(10);
  
  if T2.IsInLeapYear then
    WriteLn(Format('%d is a leap year', [T2.Year]));
  
  T1 := TDateTime.Create(1995, 2, 14); // Delphi's birthday
  
  WriteLn(Format('Delphi is %d years old today.', [T1.YearsBetween(Now)]));
  WriteLn('Delphi''s birthday was ' + T1.ToString('dd/mm/yyyy'));
end;

An important rule that I have applied in creating this record helper is that TDateTime is a value type and therefore is immutable. This means all helper methods will create and return a new value and not change the current value of the calling TDateTime variable (unless explicitly assigned).

Example

T1 := TDateTime(2012, 09, 14);

WriteLn(T1.AddYears(2).ToString); // will return 14/09/2014
WriteLn(T1.ToString);             // T1 is unchanged and will still be 14/09/2012.

Update History

2013.06.24

Added IsTomorrow Added IsDayAfterTomorrow Added property write to Year, Month, Day, Hour Minute, Second, MilliSecond Added MS alias to MilliSecond Added Format alias to ToString (for developer who are not use to toString function name)

To Do

Need to add unit tests!

Contribute

I'm open to any new ideas to make this helper record be more consistent in every day use. Please clone the repo and send your pull requests if you would like to add some unit tests, new methods, and any other ideas that would make TDateTimeHelper indispensible.

tdatetimehelper's People

Contributors

colinj avatar dkstar88 avatar

Stargazers

El Salvador avatar Me avatar wyrover avatar Jerry avatar

Watchers

 avatar Charalampos Michael avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.