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.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Language FeatureLearn C++

How To Use Skia Shader SkSL Shading Language Code in C++ Builder?

Artificial Intelligence TechC++C++17C++20Learn C++

How To Create Simple Generative AI Code In C++

C++C++17Language FeatureLearn C++

How To Use The SVG Image Format With Skia In C++ Builder

C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?