Visual Scripting: How to display date and current time with DateTime

In this article, we will explain how to use the DateTime class in Unity’s Visual Scripting to obtain date and time information.

What is DateTime?

In Unity, DateTime is a class provided by C#’s standard library for handling date and time information. It is useful for managing time.

Preparation

  1. Place an empty GameObject in Unity’s scene.
  2. Attach “Script Machine” to it from Add Component and create a new “Script Graph”.
  3. Open Menu → Edit → Project Settings, select Visual Scripting, open TypeOption, and press the + button in the lower left.
  4. Add DateTime and TimeSpan, then press Regenerate Nodes.
Image2

Get and Display the Current Time in Console

The following nodes can be used to display the current time:

  • Date Time: Get Now
  • Using this, you can get the current time.
  • Then, convert it to a String type and display it in the Console using Debug.Log.

Point: ToString is not necessary, but it is added to facilitate text display.

Image3

Extract Year, Hour, Minute, and Second from the Current Time

Year: Get Year

Image10

Hour: Get Hour

Image11

Minute: Get Minute

Image6

Second: Get Second

Image9

These nodes allow you to obtain individual time components.

Specify a Format for Time Display

  1. Add the Date Time: To String (Format) node.
  2. Enter the desired output format in the “Format” field. For example:
    • yyyy/MM/dd H:mm:ss
Image1

You can specify the output format in the Format field.

For this example, we will use [yyyy/MM/dd H:mm:ss].

yyyy Year
MM Month
dd Day
H Hour
mm Minute
ss Second

The output follows the specified format. For example, if “yyyy” is changed to “yy”, 2025 will be displayed as 25.

Image8

After execution, the current time is displayed in the Console.

Image5

Shift Time from the Current Time

You can shift the time by adding Time Span to Date Time.

Required Nodes

  1. Add Time Span Create: Time Span (Day, Hour, Minutes, Seconds).
  2. For example, set it to shift the current time by 1 hour.
Image4

In this case, we shift the current time by 1 hour.

Image7

When executed, the time shifted by 1 hour is obtained.

Image12

The time was successfully shifted by 1 hour.

Conclusion

By using Visual Scripting, you can easily manipulate date and time information. The methods introduced in this article allow you to display time, customize formats, and even perform time calculations. Apply these techniques to implement timers or event scheduling in your game.