Site icon Learn C++

Everything You Need To Use Dates Properly In C++ Software

Everything You Need To Use Dates Properly In C++ Software

How can we use DateTime correctly in our C++ software? How can we display date in format in C++ Builder? How can we get each date and time property individually such as hours, mins, or seconds?

We have discussed Learn to Use Date and Its Properties in C++” before. All the examples in that article are able to run in C++ Builder too since they stick to CLANG standards. In this post, we will also list some of time-based methods and properties.

What sort of date and time options can I use in my C++ software?

In C++ Builder and Delphi there is great time support with System.TDateTime, System.TDate, System.TTime, System.SysUtils.TTimeStamp and System.DateUtils.TTimeZone datatypes are used. These classes have many useful properties and methods to setup and display date and time properties. This rich support is really important when you are getting input or setting output in time format in a locale format, when setting time or date or date and time variable of a database field. Reading and Printing time correctly is very important when you develop global focused application, means supporting international locales and formats. All types, routines, and variables that add Date and Time support is listed here well.

TDate Is used to represent a date, it represents a special type of TDateTime value that has no decimal part. A TDate value represents the number of days that have elapsed since 12/30/1899. Here is a C++ Builder example to set DateTime(),

We can use TDateTime to define both date and time properties in this syntax below.

How to get the date and time of the system or device on which your C++ app is running?

C++ Builder has many options to set a DateTime, we can easily use Time() function to obtain system date and time from the system clock. This example below gets date and time values from the system clock and adds to lines of Memo1 (TMemo) component as below,

[crayon-6742caf241073805128036/]

If you just want to get Date we can use Date() function to obtain as given example below,

[crayon-6742caf24107c504795547/]

How to set a DateTime variable?

We can set full date and time properties of a TDateTime property with this syntax below,

[crayon-6742caf24107e823606535/]

Here is a example to set date and time and display in a TMemo component,

[crayon-6742caf24107f413309140/]

We can use DateToStr() format to display as below

[crayon-6742caf241080688316252/]

We can set a datetime object with StrToDate() function as below,

[crayon-6742caf241081016734967/]

How to display a formatted date?

In C++ Builder there is a FormatString method to display date and/or time in a specific format, full format codes are listed here. to write time in a very specific format. For example we can use ;
dd for the days,
mm or mmm for the month,
yy or yyyy for year,
hh for the hour,
nn for minutes,
ss for the seconds,
zzz for the milliseconds,

[crayon-6742caf241082461236215/]

As you see C++ Builder (also Delphi) programming language has great Date and Time format methods and properties.

Why not download a free trial of C++ Builder and try these examples today?

Exit mobile version