转自:http://www.narkii.com/club/thread-367980-1.html
///
/// 获取当前时间戳
///
///
为真时获取10位时间戳,为假时获取13位时间戳.
///
public static long GetTimeStamp(bool bflag = true)
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
long ret;
if (bflag)
ret = Convert.ToInt64(ts.TotalSeconds);
else
ret = Convert.ToInt64(ts.TotalMilliseconds);
return ret;
}
static DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
public static string NormalizeTimpstamp0(long timpStamp)
{
long unixTime = timpStamp * 10000000L;
TimeSpan toNow = new TimeSpan(unixTime);
DateTime dt = dtStart.Add(toNow);
return dt.ToString("yyyy-MM-dd");
}
///
/// 时钟式倒计时
///
///
///
public string GetSecondString(int second)
{
return string.Format("{0:D2}", second / 3600) + string.Format("{0:D2}", second % 3600 / 60) + ":" + string.Format("{0:D2}", second % 60);
}
/// 将Unix时间戳转换为DateTime类型时间
///
///
double 型数字
///
DateTime
public static System.DateTime ConvertIntDateTime(double d)
{
System.DateTime time = System.DateTime.MinValue;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0));
Debug.Log(startTime);
time = startTime.AddSeconds(d);
return time;
}
///
/// 将c# DateTime时间格式转换为Unix时间戳格式
///
///
时间
///
double
public static double ConvertDateTimeInt(System.DateTime time)
{
double intResult = 0;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
intResult = (time - startTime).TotalSeconds;
return intResult;
}
///
/// 日期转换成unix时间戳
///
///
///
public static long DateTimeToUnixTimestamp(DateTime dateTime)
{
var start = new DateTime(1970, 1, 1, 0, 0, 0, dateTime.Kind);
return Convert.ToInt64((dateTime - start).TotalSeconds);
}
///
/// unix时间戳转换成日期
///
///
时间戳(秒)
///
public static DateTime UnixTimestampToDateTime(DateTime target, long timestamp)
{
DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, target.Kind);
return start.AddSeconds(timestamp);
}