unity 关于计算时间的差值 时间戳

博主在做游戏的过程中,需要做一个时间差值的提示   在网上找打了 方法 自己进从了 整理   

 

如下

 

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);
		}
	}
}

 

 

 

 

 

 

同事说可以加上一句   记得点赞哦 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值