유니티

[유니티] DateTime.Utc로 시간 기록/시분초 구현하기

HorseDragon 2023. 3. 16. 22:54

타이머를 만들기 위해서 TimeSetup.cs를 만들었음.

여기서 DateTime.UtcNow를 이용해서 세계 표준시를 가져올 수 있다.

using System;
using UnityEngine;

public class TimeSetup : MonoBehaviour
{
    public static TimeSpan timestamp;
    
    public static int CheckTime()
    {
        timestamp = DateTime.UtcNow - new DateTime(2000,1,1,0,0,0); //2000년 1월 1일 0시 기준 현재의 time
        int timesecond = (int)timestamp.TotalSeconds; //시간을 초로 환산
        return timesecond;
    }
}

위에서 정해둔 CheckTime() 메소드를 활용하고, 시간을 분/초로 적어두기 위해서는 아래와 같이 구현해야 한다.

int time = ButtonTime + currentTime - TimeSetup.CheckTime();
int minute = (int)time/60;
int second = (int)time%60;
timer.text = string.Format("{0:D2}:{1:D2}",minute,second);