Unity gets and formats time

In Unity, you can use the DateTime structure to obtain and format time. For example, obtain the format of 13:43:15 000 on May 16, 2023, accurate to milliseconds.

using System;
using UnityEngine;

public class GetFormattedTime : MonoBehaviour
{
    private void Start()
    {
        DateTime now = DateTime.Now;
        string formattedTime = now.ToString("yyyy 年 M 月 d 日 HH:mm:ss.fff");
        Debug.Log("Formatted Time: " + formattedTime);
    }
}

In the above example, DateTime.Now gets the current date and time. Then, use the ToString method to format it into the specified format. The format string "yyyy year M month d day HH:mm:ss.fff" formats the date and time in the required format, where:

yyyy represents the 4-digit year
M represents the month (1 to 12)
d represents the number of days in the month
HH represents the hour in the 24-hour format
mm represents the minute
ss represents the second
fff represents the millisecond

Guess you like

Origin blog.csdn.net/qq_42917662/article/details/130768615