gpt4 book ai didi

c# - 我如何着手将时间变成公共(public)整数?

转载 作者:太空宇宙 更新时间:2023-11-03 13:09:08 25 4
gpt4 key购买 nike

如何将时间转换为公共(public)整数,我需要将 TimeSpan 差值转换为秒,然后将其保存为公共(public)整数。

如 4 小时 22 分 34 秒进入 15754。

    using UnityEngine;
using System.Collections;
using System;

public class Timeload : MonoBehaviour {

DateTime currentDate;
DateTime oldDate;


void Start()
{
//Store the current time when it starts
currentDate = System.DateTime.Now;

//Grab the old time from the player prefs as a long
long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));

//Convert the old time from binary to a DataTime variable
DateTime oldDate = DateTime.FromBinary(temp);
print("oldDate: " + oldDate);

//Use the Subtract method and store the result as a timespan variable
TimeSpan difference = currentDate.Subtract(oldDate);
print("Difference: " + difference);


}

void OnApplicationQuit()
{
//Savee the current system time as a string in the player prefs class
PlayerPrefs.SetString("sysString", System.DateTime.Now.ToBinary().ToString());

print("Saving this date to prefs: " + System.DateTime.Now);
}




}

最佳答案

您可以使用 TotalSeconds 属性:

//Use the Subtract method and store the result as a timespan variable
TimeSpan difference = currentDate.Subtract(oldDate);
print("Difference: " + difference.TotalSeconds);

关于c# - 我如何着手将时间变成公共(public)整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29812247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com