JS时间秒转年月日实现

分类:苦逼猿 标签:编程

公司里同事不知从哪搞来一堆代码, 大功能能实现, 但是在用到
var x = new Date(y)
的时候总是返回NaN, 于是乎, 吾自己实现了一个时间转换代替Date.


	//判断是否闰年, 闰年返回1, 平年返回0
	function isleap(y)
	{
	  if(((y) % 4) == 0 && ((y) % 100) != 0 || ((y) % 400) == 0)
		return 1;
	  else
		return 0;
	}
	
	//timer: 从格林时间到现在的秒数
	//tzoffset: 时区, 默认为8(北京)
	//返回"yyyy-mm-dd HH:MM:SS"格式的字符串
	function getTimeStr(timer, tzoffset)
	{		
		var SECS_PER_MIN = 60;
		var MINS_PER_HOUR = 60;
		var HOURS_PER_DAY =	24;
		var DAYS_PER_WEEK =	7;
		var DAYS_PER_NYEAR = 365;
		var DAYS_PER_LYEAR = 366;
		var SECS_PER_HOUR = (SECS_PER_MIN * MINS_PER_HOUR);
		var SECS_PER_DAY = ( SECS_PER_HOUR * HOURS_PER_DAY);
		var MONS_PER_YEAR =	12 ;

		var TM_SUNDAY = 0;
		var TM_MONDAY = 1;
		var TM_TUESDAY = 2;
		var TM_WEDNESDAY = 3;
		var TM_THURSDAY = 4;
		var TM_FRIDAY = 5;
		var TM_SATURDAY = 6;

		var TM_JANUARY = 0;
		var TM_FEBRUARY = 1;
		var TM_MARCH = 2;
		var TM_APRIL = 3;
		var TM_MAY = 4;
		var TM_JUNE = 5;
		var TM_JULY = 6;
		var TM_AUGUST = 7;
		var TM_SEPTEMBER = 8;
		var TM_OCTOBER = 9;
		var TM_NOVEBER = 10;
		var TM_DECEMBER = 11;
		var TM_SUNDAY = 0;

		var TM_YEAR_BASE = 1900;

		var EPOCH_YEAR = 1970;
		var EPOCH_WDAY = TM_THURSDAY;
		var mon_lengths = [[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]];
		var year_lengths = [DAYS_PER_NYEAR, DAYS_PER_LYEAR];
		
		tzoffset = ((typeof(tzoffset)=="undefined")?8:tzoffset) * MINS_PER_HOUR * SECS_PER_MIN;
		
		var days;
		var rem;
		var y;
		var yleap;
		var ip;
		
		var tm_sec;
		var tm_min;
		var tm_hour;
		var tm_mday;
		var tm_mon;
		var tm_year;
		var tm_wday;
		var tm_yday;
		var tm_isdst;
		
		days = timer / SECS_PER_DAY;
		rem = timer % SECS_PER_DAY;
		rem += tzoffset;
		while (rem < 0)
		{
			rem += SECS_PER_DAY;
			--days;
		}

		while (rem >= SECS_PER_DAY)
		{
			rem -= SECS_PER_DAY;
			++days;
		}
		tm_hour = (rem / SECS_PER_HOUR);

		rem = rem % SECS_PER_HOUR;
		tm_min = (rem / SECS_PER_MIN);
		tm_sec = (rem % SECS_PER_MIN);
		tm_wday = ((EPOCH_WDAY + days) % DAYS_PER_WEEK);

		if (tm_wday < 0)
			tm_wday += DAYS_PER_WEEK;

		y = EPOCH_YEAR;

		if (days >= 0)
		{
			for ( ; ; )
			{
				yleap = isleap(y);
				if (days < year_lengths[yleap])
					break;
				++y;
				days = days - year_lengths[yleap];
			}
		}
		else
		{
			do
			{
				--y;
				yleap = isleap(y);
				days = days + year_lengths[yleap];
			} while (days < 0);
		}
		
		tm_year = y - TM_YEAR_BASE;
		tm_yday = days;
		ip = mon_lengths[yleap];

		for (tm_mon = 0; days >= ip[tm_mon]; ++(tm_mon))
		{
			days = days - ip[tm_mon];
		}
		
		tm_mday = days + 1;
		tm_isdst = 0;

		//定义字符串格式, 格式可以自己定义
		var result = parseInt(tm_year + 1900) + "-" + parseInt(tm_mon + 1) + "-" + parseInt(tm_mday) + "-" + " "
						+ parseInt(tm_hour) + ":" + parseInt(tm_min) + ":" + parseInt(tm_sec);
		return result;
	}

使用方法

<script>
	var tm = new Date();
	var t = tm.getTime();
	var ret = getTimeStr(t/1000);
	alert(ret);
</script>

多蛋疼才会去实现这种功能啊......
_(:з」∠)_
你可能还会对下列文章感兴趣:

1条评论 你不想来一发么↓ 顺序排列 倒序排列

    向下滚动可载入更多评论,或者点这里禁止自动加载

    想说点什么呢?

    您需要登录您的Google账号才能进行评论。