获取本地时间戳
///
/// 获取本地的Unix时间戳
///
///
public static int GetLocalTimeStamp()
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
DateTime dtNow = DateTime.Parse(DateTime.Now.ToString());
TimeSpan toNow = dtNow.Subtract(dtStart);
string timeStamp = toNow.Ticks.ToString();
timeStamp = timeStamp.Substring(0, timeStamp.Length - 7);
int unixtime = 0;
if (int.TryParse(timeStamp, out unixtime))
{
return unixtime;
}
return unixtime;
}
1. 通过时间戳 - 获取2016-8-12 10:07:25 时间格式
///
/// 返回2016-8-12 10:07:25
///
/// 时间戳
public static string GetDataTime(long time)
{
System.DateTime dtDateTime = new System.DateTime(1970, 1, 1);
dtDateTime = dtDateTime.AddSeconds(time).ToLocalTime();
return dtDateTime.ToString("yyyy-M-d HH:mm:ss");
}
2. 通过时间戳 - 获取 (几分钟前,几小时前 等 聊天使用) 时间格式
// 通过时间戳 - 获取时间data
public static DateTime UnixTimeStampToDateTime(long unixTimeStamp)
{
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
return dtDateTime;
} ///
/// 获取时间差desc根据时间差获取描述,比如刚刚,1分钟前,5分钟前
///
public static string GetChatTimeDesBySpan(long num)
{
DateTime dt = ClientTools.UnixTimeStampToDateTime(num);
TimeSpan span = DateTime.Now - dt;
if (span.TotalDays > 60)
return dt.ToLongDateString();
else if (span.TotalDays > 30)
return StringManager.GetInstance().GetValue("txt_yueqian");
//一个月前
else if (span.TotalDays > 14)
return StringManager.GetInstance().GetValue("txt_bangeyueqian");
//半个月前
else if (span.TotalDays > 7)
return StringManager.GetInstance().GetValue("txt_yuzhouqian");
// 1周前
else if (span.TotalDays > 1)
return string.Format(StringManager.GetInstance().GetValue("txt_jitianqian"),(int)Math.Floor(span.TotalDays));
//{0}天前
else if (span.TotalHours > 1)
return string.Format(StringManager.GetInstance().GetValue("txt_xiaoshiqian"),(int)Math.Floor(span.TotalHours));
//{0}小时前
else if (span.TotalMinutes > 1)
return string.Format(StringManager.GetInstance().GetValue("txt_fenzhongqian"),(int)Math.Floor(span.TotalMinutes));
//{0}分钟前
else if (span.TotalSeconds > 1)
return string.Format(StringManager.GetInstance().GetValue("txt_ganggang"));
//刚刚
else
return string.Format(StringManager.GetInstance().GetValue("txt_ganggang"));
//刚刚
}
3. 通过时间戳 - 获取 (和现在的时间戳的差值,进行cd显示(01:00:00) 意思是 时间戳间隔是一个小时)
这个差值还有一个获取方法! 俩个时间差 减法 就是差值
int endTime = pb.value; // 倒计时结束时间
int currentServerTime = ClientTools.GetLocalTimeStamp(); //当前时间
int 时间差 = endTime - currentServerTime;
if (时间差 <= 0)
{
//时间戳小于等于0,直接返回 "00:00:00"
return“"00:00:00"”;
}
GetTime(时间差);
static StringBuilder _TimeStrBuilder = new StringBuilder();
void GetTime(int time)
{
TimeSpan timeSpan = TimeSpan.FromSeconds(time);
// 时间差
string timeStr = GetTimeSpanStr(timeSpan);
// 返回 一个cd01:00:00 比如这个,意思是间隔一个小时
}
public static string GetTimeSpanStr(TimeSpan timeSpan)
{if (_TimeStrBuilder == null)
{
_TimeStrBuilder = new StringBuilder();
}
_TimeStrBuilder.Remove(0, _TimeStrBuilder.Length);
int tmp = (int)(timeSpan.TotalHours);
tmp = tmp >= 0 ? tmp : 0;
if (tmp < 10)
{
_TimeStrBuilder.Append("0" + tmp);
}
else
{
_TimeStrBuilder.Append(tmp);
}
if (timeSpan.Minutes < 10)
{
tmp = timeSpan.Minutes >= 0 ? timeSpan.Minutes : 0;
_TimeStrBuilder.Append(":0" + tmp);
}
else
{
tmp = timeSpan.Minutes >= 0 ? timeSpan.Minutes : 0;
_TimeStrBuilder.Append(":" + tmp);
}
if (timeSpan.Seconds < 10)
{
tmp = timeSpan.Seconds >= 0 ? timeSpan.Seconds : 0;
_TimeStrBuilder.Append(":0" + tmp);
}
else
{
tmp = timeSpan.Seconds >= 0 ? timeSpan.Seconds : 0;
_TimeStrBuilder.Append(":" + timeSpan.Seconds);
}
return _TimeStrBuilder.ToString();
}
【【时间戳】】