C# \/date(1498820611133+0800)\/ 转DateTime

开发中经常遇到日期转换问题,特别是做接口的时候,现在整理了下时间戳转为C#格式时间的方法:

 /// <summary>
/// 时间戳转为C#格式时间
/// </summary>
/// <param name="timeStamp"></param>
/// <returns></returns>
public string GetTime(string timeStamp)
{
//处理字符串,截取括号内的数字
var strStamp = Regex.Matches(timeStamp, @"(?<=\()((?<gp>\()|(?<-gp>\))|[^()]+)*(?(gp)(?!))").Cast<Match>().Select(t => t.Value).ToArray()[].ToString();
//处理字符串获取+号前面的数字
var str = Convert.ToInt64(strStamp.Substring(, strStamp.IndexOf("+")));
long timeTricks = new DateTime(, , ).Ticks + str * + TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).Hours * * (long);
return new DateTime(timeTricks).ToString("yyyy-MM-dd HH:mm:ss"); }
/// <summary>
/// DateTime时间格式转换为Unix时间戳格式
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public int ConvertDateTimeInt(System.DateTime time)
{ System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(, , )); return (int)(time - startTime).TotalSeconds; }
上一篇:Python入门之用Python统计代码行


下一篇:微信公众号验证TOKEN