<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>闰年 _ 扒房网</title>
	<atom:link href="https://www.gxjlyf.com/tag/17438/feed" rel="self" type="application/rss+xml" />
	<link>https://www.gxjlyf.com</link>
	<description>手机系统教程_手机软件教程_手机app使用教程_电脑软件教程_电脑系统教程</description>
	<lastBuildDate>Tue, 29 Nov 2022 04:02:28 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>我来教你时间戳如何转换为日期格式</title>
		<link>https://www.gxjlyf.com/871489.html</link>
		
		<dc:creator><![CDATA[bafang18]]></dc:creator>
		<pubDate>Tue, 29 Nov 2022 04:02:28 +0000</pubDate>
				<category><![CDATA[PC教程]]></category>
		<category><![CDATA[时间]]></category>
		<category><![CDATA[闰年]]></category>
		<guid isPermaLink="false">https://www.chuwenyu.com/871489.html</guid>

					<description><![CDATA[时间戳如何转换为日期格式？当初把日期格式转换为时间戳是为了更好的记录数据，现在如果想要看看当初时间戳被转换的时 ...]]></description>
										<content:encoded><![CDATA[<p><strong></strong>时间戳如何转换为日期格式？当初把日期格式转换为时间戳是为了更好的记录数据，现在如果想要看看当初时间戳被转换的时间，可以按照以下方法来实现，详情请阅读下文MySQL、C#、JS时间戳转换方法。</p>
<p><img decoding="async" alt="时间戳如何转换为日期格式？MySQL、C#、JS时间戳转换方法" src="https://dz.cwhello.com/wp-content/uploads/2022/11/20221129040226-63858452dcbd8.jpg" /></p>
<p><strong>MySQL、C#、JS时间戳转换方法：</strong></p>
<p><strong>一、MySQL戳转换方法：</strong></p>
<p>1、原理：</p>
<p>时间戳的原理是把时间格式转为十进制格式，这样就方便时间的计算，如：1377216000000 转化后是 2013年08月23日 。</p>
<p>2、步骤：</p>
<p>(1) 创建 DateUtilsl类。</p>
<p>(2) 输入代码：</p>
<p>01importjava.text.ParseException;02importjava.text.SimpleDateFormat;03importjava.util.Date;04/*05* @author Msquirrel06*/07public class DateUtils {08privateSimpleDateFormat sf = null;09/*获取系统时间 格式为：&quot;yyyy/MM/dd &quot;*/10public static String getCurrentDate() {11Date d = newDate();12sf = newSimpleDateFormat(&quot;yyyy年MM月dd日&quot;);13returnsf.format(d);14}15/*时间戳转换成字符窜*/16public static String getDateToString(long time) {17Date d = newDate(time);18sf = newSimpleDateFormat(&quot;yyyy年MM月dd日&quot;);19returnsf.format(d);20}21/*将字符串转为时间戳*/22public static long getStringToDate(String time) {23sdf = newSimpleDateFormat(&quot;yyyy年MM月dd日&quot;);24Date date = newDate();25try{26date = sdf.parse(time);27} catch(ParseException e) {28// TODO Auto-generated catch block29e.printStackTrace();30}31returndate.getTime();32}复制代码importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;/** @author Msquirrel*/public class DateUtils {privateSimpleDateFormat sf = null;/*获取系统时间 格式为：&quot;yyyy/MM/dd &quot;*/public static String getCurrentDate() {Date d = newDate();sf = newSimpleDateFormat(&quot;yyyy年MM月dd日&quot;);returnsf.format(d);}/*时间戳转换成字符窜*/public static String getDateToString(long time) {Date d = newDate(time);sf = newSimpleDateFormat(&quot;yyyy年MM月dd日&quot;);returnsf.format(d);}/*将字符串转为时间戳*/public static long getStringToDate(String time) {sdf = newSimpleDateFormat(&quot;yyyy年MM月dd日&quot;);Date date = newDate();try{date = sdf.parse(time);} catch(ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}returndate.getTime();}</p>
<p>3、在对应使用的地方调用：</p>
<p>01DateUtils.getCurrentDate(); //获取系统当前时间02DateUtils.getDateToString(时间戳); //时间戳转为时间格式03DateUtils.getStringToDate(&quot;时间格式&quot;);//时间格式转为时间戳.复制代码DateUtils.getCurrentDate(); //获取系统当前时间DateUtils.getDateToString(时间戳); //时间戳转为时间格式DateUtils.getStringToDate(&quot;时间格式&quot;);//时间格式转为时间戳.</p>
<p><strong>二、C#时间戳转换方法：</strong></p>
<p><strong></strong>C#的代码（加入了闰年）：</p>
<p><strong>注：</strong>.Net的DateTime对象返回的是100纳秒的时间单位，年份是从AD1开始计算的。</p>
<p>01class Program02{03// 定义必须变量04const int _1M = 60; // 分钟05const int _1H = _1M * 60; // 小时06const int _1D = _1H * 24; // 天07const long _1Y = _1D * 365; // 年(非闰年)08const long _YS = _1Y * 3 + _1D * 366; // 一个闰年年度09const long _30D = _1D * 30; // 30天(月)10const long _31D = _1D * 31; // 31天(月)11const long _28D = _1D * 28; // 28天(月)12const long _29D = _1D * 29; // 29天(月)13long[] NormalYear = { _31D, _28D, _31D, _30D, _31D, _30D, _31D, _31D, _30D, _31D, _30D, _31D }; // 年14long[] LeapYear = { _31D, _29D, _31D, _30D, _31D, _30D, _31D, _31D, _30D, _31D, _30D, _31D }; // 闰年15static void Main(string[] args)16{17Program P = new Program();18System.Console.WriteLine(P.getDate(P.getTimeSpame()));19DateTime T = DateTime.Now;20System.Console.WriteLine(P.getTimeSpame() + &quot; : &quot; + P.getTimeSpame(T.Year, T.Month, T.Day, T.Hour, T.Minute, T.Second));21System.Console.ReadKey();22}23private Program() {}24public string getDate(long TimeSp)25{26// 年,月,天,小时,分钟,秒27int year = 0;28int month = 0;29int day = 0;30int hour = 0;31int minute = 0;32int second = 0;33//DateTime now = DateTime.Now;34//long TimeSp = getTimeSpame(); // 当前时间戳35// 年36int _y1 = (int)(TimeSp / _YS); // 获得按年度得到的年度37TimeSp -= _YS * _y1; // 计算剩余秒38int _y2 = (int)(TimeSp / _1Y); // 剩余年39TimeSp -= _1Y * _y2;40year = _y1 * 4 + _y2 + 1970;41// 月42long[] YearArr = isLeapYear(year) ? LeapYear : NormalYear; // 获取年的月度表43month = 1; // 从1月开始计算44for (int i = 0; i &lt; YearArr.Length; i++)45{46if (TimeSp - YearArr[i] &lt; 0) break;47++month;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 + &quot;年&quot; + month + &quot;月&quot; + day + &quot;日 &quot; + hour + &quot;点&quot; + minute + &quot;分&quot; + second + &quot;秒&quot;;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 &lt; 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() + &quot; : &quot; + 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 &lt; YearArr.Length; i++){if (TimeSp - YearArr[i] &lt; 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 + &quot;年&quot; + month + &quot;月&quot; + day + &quot;日 &quot; + hour + &quot;点&quot; + minute + &quot;分&quot; + second + &quot;秒&quot;;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 &lt; Month - 1; i++){val += YearArr[i];}Year -= 1970;val += (Year / 4) * _YS;Year -= (int)(Year / 4) * 4;val += Year * _1Y;return val;}}</p>
<p><strong>三、JS时间戳转换方法：</strong></p>
<p><strong></strong>代码如下：</p>
<p>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 + _y2 + 1970;25// 月26var YearArr = year % 4 == 0 ? LeapYear : NormalYear; // 获取年的月度表27month = 1; // 从1月开始计算28for (i=0; i&lt;YearArr.length; i++)29{30if (TimeSp - YearArr[i] &lt; 0) break;31++month;32TimeSp -= YearArr[i];33}34// 天35day = parseInt(TimeSp / _1D);36TimeSp -= day * _1D;37// 时38hour = parseInt(TimeSp / _1H);39TimeSp -= hour * _1H;40// 分41minute = parseInt(TimeSp / _1M);42// 秒43second = parseInt(TimeSp % _1M);44var DateStr = year + &quot;年&quot; + month + &quot;月&quot; + day + &quot;日 &quot; + hour + &quot;点&quot; + minute + &quot;分&quot; + second + &quot;秒&quot;;45alert(DateStr);复制代码// 定义常量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&lt;YearArr.length; i++){if (TimeSp - YearArr[i] &lt; 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 + &quot;年&quot; + month + &quot;月&quot; + day + &quot;日 &quot; + hour + &quot;点&quot; + minute + &quot;分&quot; + second + &quot;秒&quot;;alert(DateStr);</p>
<p><strong>注：</strong>JS的Date对象的getTime()方法返回的是UTC的时间戳，可以使用getTimezoneOffset()的方法来返回与UTC时差的分钟。</p>
<p><strong></strong>以上代码便是MySQL、C#、JS的时间戳转换方法介绍，因为闰年的存在，代码的使用前请阅读一下。</p>

<p><img src="https://www.gxjlyf.com/postviews/871489.png" /></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
