js unix时间戳转换

http://blog.sina.com.cn/s/blog_565826ac0101cvnb.html



用Ajax从后台传回Json数据,日期字符串Date(-150537600000)的转换:
function cTime(t) {
                    if (t != "") {
                        var time=parseInt(t.substring(6, t.length - 2));//截取字符串
                        commonTime= new Date(time);
                        returnreturn commonTime.pattern("yyyy-MM-dd");
                    }
                }

输出结果:1965-03-26

date.pattern("yyyy-MM-dd")函数:转自 http://blog.csdn.net/hemingwang0902/article/details/4511130

其他相关知识:

一、unix时间戳转普通时间:

1
2
3
4
var  unixtime=1358932051;
var  unixTimestamp  new  Date(unixtime* 1000); 
commonTime unixTimestamp.toLocaleString();
alert( "普通时间为:" +commonTime);

二、普通时间转unix时间戳

1
2
3
4
5
var  str = "2013-01-01 00:00:00" ;
str str.replace(/-/g, "/" );
var  date  new  Date(str); 
var  humanDate  new  Date(Date.UTC(date.getFullYear(),date.getMonth(),date.getDate(),date.getHours(),date.getMinutes(), date.getSeconds())); 
alert(humanDate.getTime()/1000 8*60*60);                              




===============================时间戳比较大小,前提10分 发起活动

form:

<form id="form" action="{$post_url}" method="post" class="form-horizontal form-center" name="myform">

<button class="btn submit-btn ajax-post" id="submit" type="submit" target-form="form-horizontal" style="margin-left:126px;"  οnclick="checktime(this.form)">{$submit_name|default='确 定'}</button>
</form>



js:

<script type="text/javascript">
       function checktime(form){
	  //当前时间戳
	    var timestamp = Date.parse(new Date());
            timestamp = timestamp / 1000;
			//alert(timestamp);

           //用户填写的活动开始时间戳
	     var starttime= form.act_starttime.value;  //字符型日期
             var str =starttime;
             str = str.replace(/-/g,"/");
             var date = new Date(str); 
             var humanDate = new Date(Date.UTC(date.getFullYear(),date.getMonth(),date.getDate(),date.getHours(),date.getMinutes(), date.getSeconds())); 
             user_starttime = (humanDate.getTime()/1000 - 8*60*60);  
		  
			//alert((user_starttime-timestamp)/60<10);
			 if((user_starttime-timestamp)/60<10){
				alert("活动必须提前10分钟发起");
				return false;
			}
        
		 }

		 

</script>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值