一个有态度的博客

Attitude is everything。

c# asp.net 时间,时间戳格式化互转函数。

c# asp.net 时间,时间戳格式化互转函数。

系统时间转换成UNIX时间戳,并返回

////// 系统时间转换成UNIX时间戳,并返回
        //////public static string ToUnixTime(DateTime dtime){
            //var dtime = DateTime.Now;
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            DateTime dtNow = DateTime.Parse(dtime.ToString());
            TimeSpan toNow = dtNow.Subtract(dtStart);
            string timeStamp = toNow.Ticks.ToString();
            timeStamp = timeStamp.Substring(0, timeStamp.Length - 7);
            return timeStamp;
        }
将UNIX时间戳转换成系统时间+格式,想要什么格式可以自己添加


////// 将UNIX时间戳转换成系统时间
        //////时间截///时间格式:如yyyymmddhhmmss yyyy-mm-dd hh:mm:ss///public static string UnixTimetostring(string strdatetime, string caseSwitch)
        {
            //string timeStamp = "1144821796";
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(strdatetime + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            DateTime dtResult = dtStart.Add(toNow);
            string Year = dtResult.Year.ToString();//年
            string Month = add0(dtResult.Month);//月
            string Day = add0(dtResult.Day);//日
            string Hour = add0(dtResult.Hour);//时
            string Minute = add0(dtResult.Minute);//分
            string Second = add0(dtResult.Second);//秒
 
            string stringtime = "";
            switch (caseSwitch)
            {
                case "yyyymmddhhmmss":
                    stringtime = Year + Month + Day + Hour + Minute + Second;
                    break;
                case "yyyymmddhhmm":
                    stringtime = Year + Month + Day + Hour + Minute;
                    break;
                case "yyyymmddhh":
                    stringtime = Year + Month + Day + Hour;
                    break;
                case "yyyymmdd":
                    stringtime = Year + Month + Day;
                    break;
                case "yyyymm":
                    stringtime = Year + Month;
                    break;
                case "yyyy":
                    stringtime = Year + Month;
                    break;
                case "yyyy-mm-dd hh:mm:ss":
                    //2013-12-08 07:14:57
                    stringtime = Year +"-"+ Month +"-"+ Day +" "+ Hour +":"+ Minute+ ":" +Second;
                    break;
                case "yyyy-mm-dd hh:mm":
                    //2013-12-08 07:14
                    stringtime = Year + "-" + Month + "-" + Day + " " + Hour + ":" + Minute;
                    break;
                case "yyyy-mm-dd":
                    //2013-12-08
                    stringtime = Year + "-" + Month + "-" + Day;
                    break;
                case "yyyy-mm":
                    //2013-12
                    stringtime = Year + "-" + Month;
                    break;
                case "mm-dd":
                    //12-08
                    stringtime =  Month + "-" + Day;
                    break;
                default:
                    stringtime = dtResult.ToString();
                    break;
            }
 
            return stringtime;
        }


青山之水
http://blog.nnnv.cn/index.php/archives/493

0 条评论

登录后才能发表评论!