网站首页 软件下载 游戏下载 翻译软件 电子书下载 电影下载 电视剧下载 教程攻略
标题 | 时间戳如何转换为日期格式?MySQL、C#、JS时间戳转换方法 |
分类 | 软件教程 |
内容 | 时间戳如何转换为日期格式?当初把日期格式转换为时间戳是为了更好的记录数据,现在如果想要看看当初时间戳被转换的时间,可以按照以下方法来实现,详情请阅读下文MySQL、C#、JS时间戳转换方法。 ![]() MySQL、C#、JS时间戳转换方法: 一、MySQL戳转换方法: 1、原理: 时间戳的原理是把时间格式转为十进制格式,这样就方便时间的计算,如:1377216000000 转化后是 2013年08月23日 。 2、步骤: (1) 创建 DateUtilsl类。 (2) 输入代码:
importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date; /** @author Msquirrel*/public class DateUtils { privateSimpleDateFormat sf = null;/*获取系统时间 格式为:"yyyy/MM/dd "*/public static String getCurrentDate() {Date d = newDate();sf = newSimpleDateFormat("yyyy年MM月dd日");returnsf.format(d);} /*时间戳转换成字符窜*/public static String getDateToString(long time) {Date d = newDate(time);sf = newSimpleDateFormat("yyyy年MM月dd日");returnsf.format(d);} /*将字符串转为时间戳*/public static long getStringToDate(String time) {sdf = newSimpleDateFormat("yyyy年MM月dd日");Date date = newDate();try{date = sdf.parse(time);} catch(ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}returndate.getTime();} 3、在对应使用的地方调用:
DateUtils.getCurrentDate(); //获取系统当前时间 DateUtils.getDateToString(时间戳); //时间戳转为时间格式 DateUtils.getStringToDate("时间格式");//时间格式转为时间戳. 二、C#时间戳转换方法: C#的代码(加入了闰年): 注:.Net的DateTime对象返回的是100纳秒的时间单位,年份是从AD1开始计算的。
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 + _y2 + 1970;// 月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时间戳转换方法: 代码如下:
// 定义常量var _1M = 60; // 分钟var _1H = _1M * 60; // 小时var _1D = _1H * 24; // 天var _1Y = _1D * 365; // 年(非闰年)var _YS = _1Y * 3 + _1D * 366; // 一个闰年年度var _30D = _1D * 30; // 30天(月)var _31D = _1D * 31; // 31天(月)var _28D = _1D * 28; // 28天(月)var _29D = _1D * 29; // 29天(月)var NormalYear = [ _31D, _28D, _31D, _30D, _31D, _30D, _31D, _31D, _30D, _31D, _30D, _31D ]; // 年var LeapYear = [ _31D, _29D, _31D, _30D, _31D, _30D, _31D, _31D, _30D, _31D, _30D, _31D ]; // 闰年var Now = new Date();TimeSp = Now.getTime() / 1000;//alert(Now.getTimezoneOffset()); // 时区差TimeSp += -1 * Now.getTimezoneOffset() * _1M; // 修正UTC// 年,月,天,小时,分钟,秒var year = month = day = hour = minute = second = 0;// 年var _y1 = parseInt(TimeSp / _YS); // 获得按年度得到的年度TimeSp -= _YS * _y1; // 计算剩余秒var _y2 = parseInt(TimeSp / _1Y); // 剩余年TimeSp -= _1Y * _y2;year = _y1 * 4 + _y2 + 1970;// 月var YearArr = year % 4 == 0 ? LeapYear : NormalYear; // 获取年的月度表month = 1; // 从1月开始计算for (i=0; i<YearArr.length; i++){if (TimeSp - YearArr[i] < 0) break;++month;TimeSp -= YearArr[i];}// 天day = parseInt(TimeSp / _1D);TimeSp -= day * _1D;// 时hour = parseInt(TimeSp / _1H);TimeSp -= hour * _1H;// 分minute = parseInt(TimeSp / _1M);// 秒second = parseInt(TimeSp % _1M);var DateStr = year + "年" + month + "月" + day + "日 " + hour + "点" + minute + "分" + second + "秒";alert(DateStr); 注:JS的Date对象的getTime()方法返回的是UTC的时间戳,可以使用getTimezoneOffset()的方法来返回与UTC时差的分钟。 以上代码便是MySQL、C#、JS的时间戳转换方法介绍,因为闰年的存在,代码的使用前请阅读一下。 时间戳如何转换为日期格式?MySQL、C#、JS时间戳转换方法人碧空如洗才多(人七拼八凑才济济)22. 缘分的渡口。多少青梅往事,随了清风。多少不见不散,成为笑谈。藤花疏落的青石巷口,装点着在水伊人的婉约雅致。梦里梦外,谁是谁的不离,谁是谁的不弃?即使,光阴天天新颜。我们也无法参透生命该怎样修行,才算圆满。16. What I really want to know is whether he will go abroad next month?334.绿杨烟外晓寒轻,红杏枝头春意闹。宋祁《玉楼春》 宴山亭 赵佶山口潜行始隈[阝奥],山开旷望旋平陆。时间戳转换,时间戳转换日期格式,c#时间戳转换If the subjects for the study were randomly chosen and represent a diverse cross section of the population of shampoo users, the results will be reliable regardless of the number of participants. |
随便看 |
|
霍普软件下载网教程攻略栏目提供软件使用教程、手机游戏攻略等方法技巧。