• unity工具类篇 时间间隔帮助类(天、时、分、秒) 下篇


    using System;
    using UnityEngine;
    public class TimeTest : MonoBehaviour
    {
        DateTime startTime;
        private void Start()
        {
            //获取开始时的时间
            startTime = DateTime.Now;
        }
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.Z))
            {
                //获取当前时间,并打印当前时间与开始时间相差秒数(以秒为单位)
                DateTime curtimer = DateTime.Now;
                int timer = GetSubSeconds(startTime, curtimer);
                Debug.Log(timer);
            }
        }
        /// 
        /// 获取间隔秒数
        /// 
        /// 
        /// 
        /// 
        public int GetSubSeconds(DateTime startTimer, DateTime endTimer)
        {
            TimeSpan startSpan = new TimeSpan(startTimer.Ticks);
            TimeSpan nowSpan = new TimeSpan(endTimer.Ticks);
            TimeSpan subTimer = nowSpan.Subtract(startSpan).Duration();
            返回间隔秒数(不算差的分钟和小时等,仅返回秒与秒之间的差)
            //return subTimer.Seconds;
            //返回相差时长(算上分、时的差值,返回相差的总秒数)
            return (int)subTimer.TotalSeconds;
        }
        /// 
        /// 获取两个时间的相差多少分钟
        /// 
        /// 
        /// 
        /// 
        public int GetSubMinutes(DateTime startTimer, DateTime endTimer)
        {
            TimeSpan startSpan = new TimeSpan(startTimer.Ticks);
            TimeSpan nowSpan = new TimeSpan(endTimer.Ticks);
            TimeSpan subTimer = nowSpan.Subtract(startSpan).Duration();        
            返回相差时长(仅返回相差的分钟数)
            //return subTimer.Minutes;
            //返回相差时长(仅返回相差的总分钟数)
            return (int)subTimer.TotalMinutes;
        }
        /// 
        /// 获取两个时间的相差多少小时
        /// 
        /// 
        /// 
        /// 
        public int GetSubHours(DateTime startTimer, DateTime endTimer)
        {
            TimeSpan startSpan = new TimeSpan(startTimer.Ticks);
            TimeSpan nowSpan = new TimeSpan(endTimer.Ticks);
            TimeSpan subTimer = nowSpan.Subtract(startSpan).Duration();
            返回相差时长(仅返回相差的小时)
            //return subTimer.Hours;
    		//返回相差时长(返回相差的总小时数)
            return (int)subTimer.TotalHours;
        }
        /// 
        /// 获取两个时间的相差多少天
        /// 
        /// 
        /// 
        /// 
        public int GetSubDays(DateTime startTimer, DateTime endTimer)
        {
            TimeSpan startSpan = new TimeSpan(startTimer.Ticks);
            TimeSpan nowSpan = new TimeSpan(endTimer.Ticks);
            TimeSpan subTimer = nowSpan.Subtract(startSpan).Duration();
            返回相差时长(仅返回相差的天数)
            //return subTimer.Days;
    		//返回相差时长(返回相差的总天数)
    		return (int)subTimer.TotalDays;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
  • 相关阅读:
    Leetcode原题电话号码的字母组合的两种解法【BFS-DFS】
    Hive之函数
    java毕业设计网上租房管理源码+lw文档+mybatis+系统+mysql数据库+调试
    All in One SEO 插件提取中文描述过短解决方法
    Qt 信号和槽
    WorkPlus Meet提供高效、安全视频会议解决方案
    03 卷积操作图片
    Spark读取多目录
    Python3通过字符串访问与修改局部变量
    这些年,我与Google不得不说的那些事儿
  • 原文地址:https://blog.csdn.net/weixin_44720673/article/details/126095357