using System; using System.Collections; using UnityEngine; namespace GFGGame { public class DateUtils : SingletonBase { public const int TIME_FORMAT_1 = 1; public int MONTH_PER_YEAR = 12;//每年的月数 public int DAYS_PER_MONTH = 30;//每月的天数 public int HOURS_PER_DAY = 24;//每天的小时数 public int MUNITE_PER_HOUR = 60;//每小时的分钟数 public int SECOND_PER_MUNITE = 60;//每分钟的秒数 public int SECOND_PER_DAY = 86400;//一天的秒数 public int GetCurTime() { var utcNow = DateTime.UtcNow; var timeSpan = utcNow - new DateTime(1970, 1, 1, 0, 0, 0); return (int)timeSpan.TotalSeconds; } public string getFormatBySecond(int second, int type = 1, int showLength = 2) { string str = ""; int ms = second * 1000; switch (type) { case DateUtils.TIME_FORMAT_1: str = this.format_1(second); break; } return str; } //剩余时间大于1天返回天数,小于一天返回小时数,小于一小时返回分钟数,小于1分钟返回秒数 public string format_1(int second) { if (second / this.SECOND_PER_DAY >= 1) { return string.Format ("剩余{0}天", Mathf.Floor(second / this.SECOND_PER_DAY)); } else if(second / this.SECOND_PER_DAY<1&& second/(this.MUNITE_PER_HOUR*this.SECOND_PER_MUNITE) >= 1) { return string.Format("剩余{0}小时", Mathf.Floor(second /( this.MUNITE_PER_HOUR * this.SECOND_PER_MUNITE))); } else if (second / this.SECOND_PER_DAY < 1 && second / (this.MUNITE_PER_HOUR * this.SECOND_PER_MUNITE) < 1&& second/this. SECOND_PER_MUNITE>=1) { return string.Format("剩余{0}分钟", Mathf.Floor(second / this.SECOND_PER_MUNITE)); }else if (second< this.SECOND_PER_MUNITE) { return string.Format("剩余{0}秒", second); } return ""; } } }