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; } //direction排版方向:0横向,1纵向 public string getFormatBySecond(int second, int type = 1, int direction = 0) { string str = ""; int ms = second * 1000; switch (type) { case DateUtils.TIME_FORMAT_1: str = this.format_1(second, direction); break; } return str; } //剩余时间大于1天返回天数,小于一天返回小时数,小于一小时返回分钟数,小于1分钟返回秒数 public string format_1(int second, int direction = 0) { if (second / this.SECOND_PER_DAY >= 1) { if (direction == 0) { return string.Format("剩余{0}天", Mathf.Floor(second / this.SECOND_PER_DAY)); } else { return string.Format("剩\n余\n{0}\n天", 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) { if (direction == 0) { return string.Format("剩余{0}小时", Mathf.Floor(second / (this.MUNITE_PER_HOUR * this.SECOND_PER_MUNITE))); } else { return string.Format("剩\n余\n{0}\n小\n时", 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) { if (direction == 0) { return string.Format("剩余{0}分钟", Mathf.Floor(second / this.SECOND_PER_MUNITE)); } else { return string.Format("剩\n余\n{0}\n分\n钟", Mathf.Floor(second / this.SECOND_PER_MUNITE)); } } else if (second < this.SECOND_PER_MUNITE) { if (direction == 0) { return string.Format("剩余{0}秒", second); } else { return string.Format("剩\n余\n{0}\n秒", second); } } return ""; } } }