C++Introduction to C++Language FeatureLearn C++Syntax

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,

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

How to set a DateTime variable?

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

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

We can use DateToStr() format to display as below

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

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,

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?


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
CC++Introduction to C++Language FeatureLearn C++

Who Invented The C++ Programming Language?

CC++C++17Introduction to C++Language FeatureLearn C++

What Is Count In C Programming?

CC++Introduction to C++Language FeatureLearn C++

Why Is C The Most Popular Programming Language?

CC++C++17Introduction to C++Learn C++Syntax

How To Use Switch In C++ And C Programming?