博主在做游戏的过程中,需要做一个时间差值的提示 在网上找打了 方法 自己进从了 整理
如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;// 这个域名一定要引用
public class Chazhi_ceshi : MonoBehaviour {
DateTime time_new=DateTime.Now;//建立一个新的时间 并取得现在的时间
DateTime time_new1=new DateTime() ;//建立一个新的时间
TimeSpan ts1;//建立一个TimeSpan的变量 专门用于计算时间的差值
// Use this for initialization
void Start () {
// time_new1 = time_new;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown ("o")) {
ts1 = time_new - time_new1;//取得之间的差值 这个差值是 日(这个数值包括年月转化成的日/30=月 /360=年 /1=日) 时/分/秒 如果插值有则会这样出现
Debug.Log ("ts1.Minutes.ToString ()==" +ts1.Minutes.ToString ());//只取到分的差值
Debug.Log ("ts1.Hours.ToString ()" +ts1.Hours.ToString ());//只取到了小时
Debug.Log ("ts1.ToString ()" +ts1.ToString ());// -----这个是打印的结果 360*年+12*月+日 时/分/秒
Debug.Log ("ts1.Days.ToString ()" + ts1.Days.ToString ());//只取到日
Debug.Log ("ts1.Days / 360" + ts1.Days / 360);//取到年
Debug.Log ("(ts1.Days % 360) / 30" +(ts1.Days % 360) / 30);//取到月
Debug.Log ("(ts1.Days % 360) % 30" + (ts1.Days % 360) % 30);//取到日
string aa = string.Format ("{0:d1}-{1:d1}-{2:d1}", ts1.Days / 360, (ts1.Days % 360) / 30, (ts1.Days % 360) % 30);
Debug.Log (aa);//取到年--------------学过C语言的小伙伴有没有发现 跟C语言的显示方法差不多
Debug.Log ("time_new1===" + time_new1);
Debug.Log ("new_time===" + time_new);
}
}
}
同事说可以加上一句 记得点赞哦