时间戳如何转换为日期格式?MySQL、C#、JS时间戳转换方法( 二 )

< YearArr.Length; i)45{46if (TimeSp - YearArr[i] < 0) break;47month;48TimeSp -= YearArr[i];49}50// 天51day = (int)(TimeSp / _1D);52TimeSp -= day * _1D;53// 时54hour = (int)(TimeSp / _1H);55TimeSp -= hour * _1H;56// 分57minute = (int)(TimeSp / _1M);58// 秒59second = (int)(TimeSp % _1M);60string DateStr = year"年"month"月"day"日 "hour"点"minute"分"second"秒";61return DateStr;62}63// 判断是否闰年64private bool isLeapYear(int year)65{66return (year % 4 == 0 ? true : false);67}68// 获取当前时间戳 按1970年开始计算,精度为秒!69private long getTimeSpame()70{71DateTime _Now = DateTime.Now;72DateTime _1970 = new DateTime(1970, 1, 1);73long _Sp = (_Now.Ticks - _1970.Ticks) / 10000000;74return _Sp;75}76// 按既定格式把时间转成成时间戳77private long getTimeSpame(int Year, int Month, int Day, int Hour, int Minute, int Second)78{79long val = 0;80val= Second; // 秒81val= Minute * _1M; // 分钟82val= Hour * _1H; // 小时83val= Day * _1D; // 天84long[] YearArr = isLeapYear(Year) ? LeapYear : NormalYear;85for (int i = 0; i < Month - 1; i)86{87val= YearArr[i];88}89Year -= 1970;90val= (Year / 4) * _YS;91Year -= (int)(Year / 4) * 4;92val= Year * _1Y;93return val;94}95}复制代码class Program{// 定义必须变量const int _1M = 60; // 分钟const int _1H = _1M * 60; // 小时const int _1D = _1H * 24; // 天const long _1Y = _1D * 365; // 年(非闰年)const long _YS = _1Y * 3_1D * 366; // 一个闰年年度const long _30D = _1D * 30; // 30天(月)const long _31D = _1D * 31; // 31天(月)const long _28D = _1D * 28; // 28天(月)const long _29D = _1D * 29; // 29天(月)long[] NormalYear = { _31D, _28D, _31D, _30D, _31D, _30D, _31D, _31D, _30D, _31D, _30D, _31D }; // 年long[] LeapYear = { _31D, _29D, _31D, _30D, _31D, _30D, _31D, _31D, _30D, _31D, _30D, _31D }; // 闰年static void Main(string[] args){Program P = new Program();System.Console.WriteLine(P.getDate(P.getTimeSpame()));DateTime T = DateTime.Now;System.Console.WriteLine(P.getTimeSpame()" : "P.getTimeSpame(T.Year, T.Month, T.Day, T.Hour, T.Minute, T.Second));System.Console.ReadKey();}private Program() {}public string getDate(long TimeSp){// 年,月,天,小时,分钟,秒int year = 0;int month = 0;int day = 0;int hour = 0;int minute = 0;int second = 0;//DateTime now = DateTime.Now;//long TimeSp = getTimeSpame(); // 当前时间戳// 年int _y1 = (int)(TimeSp / _YS); // 获得按年度得到的年度TimeSp -= _YS * _y1; // 计算剩余秒int _y2 = (int)(TimeSp / _1Y); // 剩余年TimeSp -= _1Y * _y2;year = _y1 * 4_y21970;// 月long[] YearArr = isLeapYear(year) ? LeapYear : NormalYear; // 获取年的月度表month = 1; // 从1月开始计算for (int i = 0; i < YearArr.Length; i){if (TimeSp - YearArr[i] < 0) break;month;TimeSp -= YearArr[i];}// 天day = (int)(TimeSp / _1D);TimeSp -= day * _1D;// 时hour = (int)(TimeSp / _1H);TimeSp -= hour * _1H;// 分minute = (int)(TimeSp / _1M);// 秒second = (int)(TimeSp % _1M);string DateStr = year"年"month"月"day"日 "hour"点"minute"分"second"秒";return DateStr;}// 判断是否闰年private bool isLeapYear(int year){return (year % 4 == 0 ? true : false);}// 获取当前时间戳 按1970年开始计算,精度为秒!private long getTimeSpame(){DateTime _Now = DateTime.Now;DateTime _1970 = new DateTime(1970, 1, 1);long _Sp = (_Now.Ticks - _1970.Ticks) / 10000000;return _Sp;}// 按既定格式把时间转成成时间戳private long getTimeSpame(int Year, int Month, int Day, int Hour, int Minute, int Second){long val = 0;val= Second; // 秒val= Minute * _1M; // 分钟val= Hour * _1H; // 小时val= Day * _1D; // 天long[] YearArr = isLeapYear(Year) ? LeapYear : NormalYear;for (int i = 0; i < Month - 1; i){val= YearArr[i];}Year -= 1970;val= (Year / 4) * _YS;Year -= (int)(Year / 4) * 4;val= Year * _1Y;return val;}}三、JS时间戳转换方法:
代码如下:
01// 定义常量02var _1M = 60; // 分钟03var _1H = _1M * 60; // 小时04var _1D = _1H * 24; // 天05var _1Y = _1D * 365; // 年(非闰年)06var _YS = _1Y * 3_1D * 366; // 一个闰年年度07var _30D = _1D * 30; // 30天(月)08var _31D = _1D * 31; // 31天(月)09var _28D = _1D * 28; // 28天(月)10var _29D = _1D * 29; // 29天(月)11var NormalYear = [ _31D, _28D, _31D, _30D, _31D, _30D, _31D, _31D, _30D, _31D, _30D, _31D ]; // 年12var LeapYear = [ _31D, _29D, _31D, _30D, _31D, _30D, _31D, _31D, _30D, _31D, _30D, _31D ]; // 闰年13var Now = new Date();14TimeSp = Now.getTime() / 1000;15//alert(Now.getTimezoneOffset()); // 时区差16TimeSp= -1 * Now.getTimezoneOffset() * _1M; // 修正UTC17// 年,月,天,小时,分钟,秒18var year = month = day = hour = minute = second = 0;19// 年20var _y1 = parseInt(TimeSp / _YS); // 获得按年度得到的年度21TimeSp -= _YS * _y1; // 计算剩余秒22var _y2 = parseInt(TimeSp / _1Y); // 剩余年23TimeSp -= _1Y * _y2;24year = _y1 * 4_y21970;25// 月26var YearArr = year % 4 == 0 ? LeapYear : NormalYear; // 获取年的月度表27month = 1; // 从1月开始计算28for (i=0; i

推荐阅读