您的位置:首页 > 其它

DateTime

2015-06-23 18:48 190 查看
//时间、数值工具类//

public class _Utility:MonoBehaviour{

void Start () {

}

void Update () {

}

public static int getDstNum(int num)
{
return (num+198012)^654321;
}
public static int getSrcNum(int num)
{
return (num^654321)-198012;
}

//日期相关*******************************************************************************************************************//
//现在到1970的时间,精确到毫秒//
//static Stopwatch stopwatch = new Stopwatch();
public static double currentTimeMillis()
{
TimeSpan ts=DateTime.UtcNow - DateTime.Parse("1970-1-1");
return ts.TotalMilliseconds;
}
//获得当前时间DateTime对象//
public static System.DateTime getTime()
{
//System.DateTime.Now//本地时间,12小时制的//
//System.DateTime.Now.ToString("yyyyMMddHHmmss")//军事日期时间//
//System.DateTime.UtcNow//UTC时间//
//GUILayout.SelectionGrid(System.DateTime.Now.Month-1,Months,3);
//GUILayout.SelectionGrid(System.DateTime.Now.Day-1,Days,10);
//GUILayout.Box(str+dNow.Now.Year+"年"+dNow.Now.Month+"月"+dNow.Now.Day+" 日"+" "+dNow.Now.Hour+"时"+dNow.Now.Minute+"分"+dNow.Now.Second+"秒");
System.DateTime now = System.DateTime.Now;
return now;
}
//返回当前时间与oldTime的时间差,以秒为单位//
public static System.TimeSpan getSpanTime(System.DateTime oldTime)
{
System.TimeSpan span = getTime() - oldTime;
return span;
}
//返回当前时间与oldTime的时间差,以秒为单位//
public static int getSpanTimeSeconds(System.DateTime oldTime)
{
System.TimeSpan span = getTime() - oldTime;
return (int)span.TotalSeconds;
}
//当前时间到未来一个时间,还剩多少时间//
public static System.TimeSpan getRemainTime(System.DateTime futureTime)
{
System.TimeSpan remain = futureTime - getTime();
return remain;
}
//DateTime转换为19700101,单位 秒//
public static double ConvertDateTime2Int(System.DateTime time)
{
double intResult = 0;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
intResult = (time - startTime).TotalSeconds;
return intResult;
}
//19700101,单位 秒转换为DateTime//
public static System.DateTime ConvertInt2DateTime(double d)
{
System.DateTime time = System.DateTime.MinValue;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
time = startTime.AddSeconds(d);
return time;
}
//判断是不是今天//
public static bool timeIsToday(long time)
{
//+8时区//
double curTime = currentTimeMillis() / 1000;
//curTime += 8*3600;

DateTime date = ConvertInt2DateTime (time);
DateTime curdate = ConvertInt2DateTime (curTime);

if(date.Year == curdate.Year && date.Month == curdate.Month && date.Day == curdate.Day)
{
return true;
}
return false;
}
//判断是不是过了cd,参数cd,精度,次数,时间偏移,被比较的时间//
public static bool isCrossResetPoint(int cd,int accuracy,int count,int offset,int lastSecond)
{
double curTime = currentTimeMillis() / 1000;
int cur = (int)(curTime - offset);
int last = lastSecond - offset;

if (accuracy == 1) //精度1秒//
{
if((cur - last) >= cd * accuracy)
{
return true;
}
}else if(accuracy == 86400)//精度1天//
{
if(timeIsToday(lastSecond))
{
return false;
}else
{
return true;
}
}else//精度其他//
{
return false;
}
return false;
}
//某个日期是否 > 某个日期//
public static bool IsGreaterDate(DateTime myDate, DateTime compareDate)
{
return myDate.CompareTo(compareDate) >= 0;
}
//某个日期是否 < 某个日期//
public static bool IsSmallerDate(DateTime myDate, DateTime compareDate)
{
return myDate.CompareTo(compareDate) <= 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: