介紹
這里附帶一個(gè)時(shí)間戳和時(shí)間轉(zhuǎn)換的網(wǎng)址
時(shí)間戳是什么
時(shí)間戳就是從1970年1月1日0時(shí)0分0秒起到現(xiàn)在的總毫秒數(shù),為什么時(shí)1970/1/1/00:00:00,因?yàn)榈谝慌_(tái)計(jì)算機(jī)發(fā)明時(shí)間是這個(gè)時(shí)間,所以時(shí)間戳誕生了。
什么時(shí)候用時(shí)間戳
比如說(shuō)你要做一些時(shí)間相關(guān)的功能,那么基本都會(huì)用到時(shí)間戳。而且時(shí)間戳是精確的,比如說(shuō)要做計(jì)時(shí)、寶箱倒計(jì)時(shí)、賬號(hào)禁言、封號(hào)等相關(guān)問(wèn)題你請(qǐng)求服務(wù)器的數(shù)據(jù)一般都是通過(guò)時(shí)間戳來(lái)獲取具體時(shí)間。而且如果與服務(wù)器通訊也可以通過(guò)時(shí)間戳來(lái)做一個(gè)請(qǐng)求響應(yīng)時(shí)間。
獲取時(shí)間
獲取當(dāng)前時(shí)間
//北京時(shí)間
DateTime date1 = DateTime.Now;
Debug.LogError("北京時(shí)間:" + date1);
//國(guó)際時(shí)間
DateTime date2 = DateTime.UtcNow;
Debug.LogError("國(guó)際時(shí)間:" + date2);
獲取時(shí)間戳
//時(shí)間戳方法一
long now1 = DateTime.UtcNow.Ticks;
Debug.LogError("當(dāng)前時(shí)間戳:" + now1);
//時(shí)間戳方法二
long now2 = DateTime.Now.ToUniversalTime().Ticks;
Debug.LogError("當(dāng)前時(shí)間戳:" + now2);
日期轉(zhuǎn)時(shí)間戳
//方法一
TimeSpan st1 = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0);
Debug.LogError("日期轉(zhuǎn)為毫秒時(shí)間戳:" + Convert.ToInt64(st1.TotalMilliseconds));
Debug.LogError("日期轉(zhuǎn)為秒時(shí)間戳:" + Convert.ToInt64(st1.TotalSeconds));
Debug.LogError("日期轉(zhuǎn)為分鐘時(shí)間戳:" + Convert.ToInt64(st1.TotalMinutes));
Debug.LogError("日期轉(zhuǎn)為小時(shí)時(shí)間戳:" + Convert.ToInt64(st1.TotalHours));
Debug.LogError("日期轉(zhuǎn)為天時(shí)間戳:" + Convert.ToInt64(st1.TotalDays));
//方法二
TimeSpan st2 = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0);
Debug.LogError("日期轉(zhuǎn)為毫秒時(shí)間戳:" + Convert.ToInt64(st2.TotalMilliseconds));
Debug.LogError("日期轉(zhuǎn)為秒時(shí)間戳:" + Convert.ToInt64(st2.TotalSeconds));
Debug.LogError("日期轉(zhuǎn)為分鐘時(shí)間戳:" + Convert.ToInt64(st2.TotalMinutes));
Debug.LogError("日期轉(zhuǎn)為小時(shí)時(shí)間戳:" + Convert.ToInt64(st2.TotalHours));
Debug.LogError("日期轉(zhuǎn)為天時(shí)間戳:" + Convert.ToInt64(st2.TotalDays));
//方法三
double tS1 = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000);
Debug.LogError("日期轉(zhuǎn)為時(shí)間戳:" + tS1);
//方法四
double tS2 = ((DateTime.UtcNow.Ticks - 621355968000000000) / 10000);
Debug.LogError("日期轉(zhuǎn)為時(shí)間戳:" + tS2);
時(shí)間戳轉(zhuǎn)日期
//方法一
DateTime startTime1 = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Local);
DateTime dt1 = startTime1.AddMilliseconds(1698766775000);//傳入的時(shí)間戳
Debug.LogError("時(shí)間戳轉(zhuǎn)時(shí)間:" + dt1);
//方法二
DateTime startDateTime = TimeZoneInfo.ConvertTimeFromUtc(new DateTime(1970, 1, 1, 0, 0, 0), TimeZoneInfo.Local);
long targetTimeStamp = ((long)1698766775000 * 10000);
TimeSpan targetTS = new TimeSpan(targetTimeStamp);
DateTime targetDateTime = startDateTime.Add(targetTS);
Debug.LogError("時(shí)間戳轉(zhuǎn)時(shí)間:" + targetDateTime);
//方法三
DateTime startTime3 = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
DateTime dt3 = startTime3.AddTicks(1698766775000 * 10000);//傳入的時(shí)間戳
Debug.LogError("時(shí)間戳轉(zhuǎn)時(shí)間:" + dt3);
將時(shí)間戳轉(zhuǎn)換為多久之前
void Start()
{
Debug.LogError(GetTimeLongAgo(20));
Debug.LogError(GetTimeLongAgo(3000));
Debug.LogError(GetTimeLongAgo(50000));
Debug.LogError(GetTimeLongAgo(864000));
Debug.LogError(GetTimeLongAgo(25920000));
Debug.LogError(GetTimeLongAgo(61104000));
}
/// <summary>
/// 將秒數(shù)時(shí)間戳轉(zhuǎn)換為多久之前。傳入時(shí)間戳t(t= 當(dāng)前時(shí)間戳() - 指定時(shí)間的時(shí)間戳 )
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public string GetTimeLongAgo(double t)
{
string str = "";
double num;
if (t < 60)
{
str = string.Format("{0}秒前", t);
}
else if (t >= 60 && t < 3600)
{
num = Math.Floor(t / 60);
str = string.Format("{0}分鐘前", num);
}
else if (t >= 3600 && t < 86400)
{
num = Math.Floor(t / 3600);
str = string.Format("{0}小時(shí)前", num);
}
else if (t > 86400 && t < 2592000)
{
num = Math.Floor(t / 86400);
str = string.Format("{0}天前", num);
}
else if (t > 2592000 && t < 31104000)
{
num = Math.Floor(t / 2592000);
str = string.Format("{0}月前", num);
}
else
{
num = Math.Floor(t / 31104000);
str = string.Format("{0}年前", num);
}
return str;
}
星期
//方案一
string[] Day = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
string week = Day[Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"))].ToString();
Debug.LogError(week);
//方案二
Debug.LogError(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek));
//方案三
Debug.LogError(DateTime.Today.DayOfWeek.ToString());
自定義格式時(shí)間
DateTime dateTime = DateTime.Now;
string strNowTime = string.Format("{0:D}-{1:D}-{2:D}-{3:D}-{4:D}-{5:D}-{6:D}", dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Millisecond);
Debug.LogError(strNowTime);
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-751895.html
總結(jié)
這里其實(shí)還有很多中方式,上面知識(shí)其中的一些,基本是夠用了,歡迎評(píng)論區(qū)補(bǔ)充文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-751895.html
到了這里,關(guān)于Unity中獲取時(shí)間戳、日期、時(shí)間、毫秒、秒以相互轉(zhuǎn)換、自定義格式時(shí)間的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!