时间戳转换

很多地方用到这个东西, 目前我的项目里   抽奖 月卡 邮件 聊天 结算  活动等

一般这种东西都写成工具类,方便调用

这是服务器发过来long时间戳, 然后我调用一下方法  GetTime(long.tostring())

System.Text.StringBuilder mStringBuilder = new System.Text.StringBuilder ();

string GetTime (string dt){

string timeStamp = dt;

DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime (new DateTime (1970, 1, 1));

long ltime = long.Parse (timeStamp + "0000");

TimeSpan toNow = new TimeSpan (ltime);

DateTime dtResult = dtStart.Add (toNow);

string date = dtResult.ToShortDateString ().ToString ();

string time = dtResult.ToLongTimeString ().ToString ();

string[] date_arr = date.Split ('/');

string[] time_arr = time.Split (':');

mStringBuilder.Remove (0, mStringBuilder.Length);

//string result;

if (dtResult.ToString ().Contains ("PM") && time_arr [0] != "12") {

//result = date_arr [0] + "月" + date_arr [1] + "日" + (int.Parse (time_arr [0]) + 12) + "时" + time_arr [1] + "分";

mStringBuilder.Append (date_arr [2] + ".");

mStringBuilder.Append (date_arr [0] + ".");

//mStringBuilder.Append (TextData.Month);

mStringBuilder.Append (date_arr [1] + ".");

//mStringBuilder.Append (TextData.Day);

mStringBuilder.Append (((int.Parse (time_arr [0]) + 12) + ":"));

//mStringBuilder.Append (TextData.Hour);

mStringBuilder.Append (time_arr [1]);

//mStringBuilder.Append (TextData.Minute);

} else {

//result = date_arr [0] + "月" + date_arr [1] + "日" + time_arr [0] + "时" + time_arr [1] + "分";

mStringBuilder.Append (date_arr [2] + ".");

mStringBuilder.Append (date_arr [0] + ".");

//mStringBuilder.Append (TextData.Month);

mStringBuilder.Append (date_arr [1] + ".");

//mStringBuilder.Append (TextData.Day);

mStringBuilder.Append (time_arr [0] + ":");

//mStringBuilder.Append (TextData.Hour);

mStringBuilder.Append (time_arr [1]);

//mStringBuilder.Append (TextData.Minute);

}

return mStringBuilder.ToString ();

}

上边是服务器直接发过来时间戳    如果是 如何判断当前的时间和服务器发过来的大小

下面 获得当前时间long 然后对比已知 OK

GetTimeStamp (DateTime.Now)

private long GetTimeStamp (DateTime dt)

{  

DateTime dateStart = new DateTime (1970, 1, 1, 8, 0, 0);  

long timeStamp = Convert.ToInt64 ((dt - dateStart).TotalMilliseconds);  

return timeStamp;  

}

你可能感兴趣的:(时间戳转换)