教你時間戳如何轉換為日期格式( 二 )

< 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

推薦閱讀