Home› X++ Programming Language› / Programs / Print Current Date and Time
🚀 Programming Example

Print Current Date and Time

17 Views
Beginner
Code Snippet
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

💻 Program Code

/// <summary>
/// Print Current Date and Time.
/// </summary>
internal final class Ansari004_PrintDateTime
{
    public static void main(Args _args)
    {
        date        todayDate = today();
        utcdatetime nowUtc    = DateTimeUtil::utcNow();

        info(strFmt("Session date : %1", todayDate));
        info(strFmt("UTC date-time: %1", nowUtc));
        info(strFmt("System date  : %1", DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone())));
    }

}

📘 Explanation

Print Current Date and Time
Figure: Print Current Date and Time

Print Current Date and Time in X++ (today(), utcNow() and getSystemDate())

Overview

Dates and times are everywhere in ERP: posting dates, delivery dates, created and modified timestamps, batch schedules. D365 F&O has more than one idea of "now", and mixing them up is a classic source of bugs. This program prints three values side by side: the session date, the current UTC date-time, and the system date in the user's time zone.

Understanding the difference is important because D365 F&O stores every utcDateTime in UTC in the database and converts it to the user's time zone only when it is shown on screen.

Prerequisites

  • A D365 F&O development environment with Visual Studio, and a model and project to work in.
  • A runnable class: Add > New Item > Dynamics 365 Items > Code > Runnable Class (Job). The class name must match the file name.
  • No tables or setup data are needed. Your user time zone is set in Settings > User options > Preferences.

Code Walkthrough

1. Reading the session date

date        todayDate = today();

today() returns the session date as a date (no time part). The session date can be changed by the user in Settings > Session date and time, which is useful for testing month-end postings. The variable is named todayDate so it does not hide the today() function.

2. Reading the current UTC date-time

utcdatetime nowUtc    = DateTimeUtil::utcNow();

DateTimeUtil::utcNow() returns the real current moment in UTC as a utcdatetime, including hours, minutes and seconds. It ignores the session date setting. The variable names are aligned, which is a common formatting convention that makes declarations easy to scan.

3. Printing the session date and the UTC date-time

info(strFmt("Session date : %1", todayDate));
info(strFmt("UTC date-time: %1", nowUtc));

strFmt() formats dates using the user's regional settings, so 9/29/2026 may appear as 29-09-2026 for another user. Note that the UTC value is shown as stored, without a time-zone conversion.

4. Printing the system date in the user's time zone

info(strFmt("System date  : %1", DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone())));

getUserPreferredTimeZone() returns the time zone from the user options, and getSystemDate() returns today's real date in that zone. This is the value Microsoft recommends instead of today() in production code, because it is not affected by the session date and it respects the user's time zone (near midnight, the UTC date and the local date can differ).

Key Concepts

  • date vs utcdatetime: date holds only a calendar date. utcdatetime holds date and time in UTC.
  • Session date: a per-session setting that today() reads. Users can change it, so do not use it for audit timestamps.
  • DateTimeUtil class: the toolbox for utcdatetime: utcNow(), newDateTime(), addDays(), addHours(), getDifference(), applyTimeZoneOffset() and more.
  • Storage in UTC: fields such as CreatedDateTime are saved in UTC and converted for display, so users in India and the USA see the same moment in their own local time.

Expected Output

Session date : 9/29/2026
UTC date-time: 9/29/2026 02:30:00 pm
System date  : 9/29/2026

The actual values depend on when you run the program, the session date setting and your time zone. The date format follows your user options.

Best Practices and Common Mistakes

  • Do use DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone()) for "today" in business logic.
  • Do use DateTimeUtil::utcNow() for timestamps and duration calculations.
  • Do not name a variable today. It hides the built-in function.
  • Do not apply a time-zone offset before saving a value to a utcdatetime field. Save UTC, and convert only for display.
  • Remember that best-practice checks warn about today() and suggest getSystemDate().

Interview Questions

  1. What is the difference between today() and DateTimeUtil::getSystemDate()?
    today() returns the session date, which users can change. getSystemDate() returns the real date in the given time zone.
  2. In which time zone are utcdatetime fields stored?
    UTC. They are converted to the user's time zone only for display.
  3. How do you convert a UTC value to the user's time zone?
    DateTimeUtil::applyTimeZoneOffset(value, DateTimeUtil::getUserPreferredTimeZone()).
  4. How do you extract the date part from a utcdatetime?
    DateTimeUtil::date(myDateTime). Use DateTimeUtil::time() for the time part in seconds.
  5. What is the null value for utcdatetime?
    DateTimeUtil::minValue(), which is 1900-01-01 00:00:00.
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.