C#时间戳与时间互转

 

/// <summary>
        /// 时间戳转成时间类型
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static  DateTime GetTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            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 = 0;
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            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 = 0;
            foreach (KeyValuePair<string, string> jd in jsonDict)
            {
                if (i != (jsonDict.Count() - 1))
                {
                    sb.Append("\"" + jd.Key + "\":\"" + jd.Value + "\",");
                }
                else
                {
                    sb.Append("\"" + jd.Key + "\":\"" + jd.Value + "\"");
                }

                i++;
            }

            sb.Append("}");
            return sb.ToString();
        }

 

posted @ 2016-05-27 11:23  zhaozi  阅读(278)  评论(0编辑  收藏  举报