C#时间戳与时间互转

时间:2021-12-27 19:39:19
/// <summary>
/// 时间戳转成时间类型
/// </summary>
/// <param name="timeStamp"></param>
/// <returns></returns>
public static DateTime GetTime(string timeStamp)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(, , ));
long lTime = long.Parse(timeStamp + "");
TimeSpan toNow = new TimeSpan(lTime);
return dtStart.Add(toNow);
} /// <summary>
/// 时间类型转成long
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static long ConvertDateTimeInt(System.DateTime time)
{
long intResult = ;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(, , ));
intResult = (long)(time - startTime).TotalSeconds;
return intResult;
}
/// <summary>
/// 拼接json的字符串;
/// 比如:"{\"ret\":\"err\",\"stadname\":\"未知\"}"
/// </summary>
/// <param name="jsonDict"></param>
/// <returns></returns>
public static string getJsonStr(Dictionary<string, string> jsonDict)
{
StringBuilder sb = new StringBuilder("{");
int i = ;
foreach (KeyValuePair<string, string> jd in jsonDict)
{
if (i != (jsonDict.Count() - ))
{
sb.Append("\"" + jd.Key + "\":\"" + jd.Value + "\",");
}
else
{
sb.Append("\"" + jd.Key + "\":\"" + jd.Value + "\"");
} i++;
} sb.Append("}");
return sb.ToString();
}