您的位置:首页 > Web前端 > JavaScript

javascript时间戳和日期字符串相互转换

2015-12-16 11:25 681 查看
//获取当前时间戳(以s为单位)
vartimestamp=Date.parse(newDate());
timestamp=timestamp/1000;
//当前时间戳为:1403149534
console.log("当前时间戳为:"+timestamp);

//获取某个时间格式的时间戳
varstringTime="2014-07-1010:21:12";
vartimestamp2=Date.parse(newDate(stringTime));
timestamp2=timestamp2/1000;
//2014-07-1010:21:12的时间戳为:1404958872
console.log(stringTime+"的时间戳为:"+timestamp2);

//将当前时间换成时间格式字符串
vartimestamp3=1403058804;
varnewDate=newDate();
newDate.setTime(timestamp3*1000);
//WedJun182014
console.log(newDate.toDateString());
//Wed,18Jun201402:33:24GMT
console.log(newDate.toGMTString());
//2014-06-18T02:33:24.000Z
console.log(newDate.toISOString());
//2014-06-18T02:33:24.000Z
console.log(newDate.toJSON());
//2014年6月18日
console.log(newDate.toLocaleDateString());
//2014年6月18日上午10:33:24
console.log(newDate.toLocaleString());
//上午10:33:24
console.log(newDate.toLocaleTimeString());
//WedJun18201410:33:24GMT+0800(中国标准时间)
console.log(newDate.toString());
//10:33:24GMT+0800(中国标准时间)
console.log(newDate.toTimeString());
//Wed,18Jun201402:33:24GMT
console.log(newDate.toUTCString());

Date.prototype.format=function(format){
vardate={
"M+":this.getMonth()+1,
"d+":this.getDate(),
"h+":this.getHours(),
"m+":this.getMinutes(),
"s+":this.getSeconds(),
"q+":Math.floor((this.getMonth()+3)/3),
"S+":this.getMilliseconds()
};
if(/(y+)/i.test(format)){
format=format.replace(RegExp.$1,(this.getFullYear()+'').substr(4-RegExp.$1.length));
}
for(varkindate){
if(newRegExp("("+k+")").test(format)){
format=format.replace(RegExp.$1,RegExp.$1.length==1
?date[k]:("00"+date[k]).substr((""+date[k]).length));
}
}
returnformat;
}
console.log(newDate.format('yyyy-MM-ddh:m:s'));

</script>

1、当前时间换时间戳



vartimestamp=parseInt(newDate().getTime()/1000);//当前时间戳
document.write(timestamp);





2、当前时间换日期字符串



varnow=newDate();
varyy=now.getFullYear();//年
varmm=now.getMonth()+1;//月
vardd=now.getDate();//日
varhh=now.getHours();//时
varii=now.getMinutes();//分
varss=now.getSeconds();//秒
varclock=yy+"-";
if(mm<10)clock+="0";
clock+=mm+"-";
if(dd<10)clock+="0";
clock+=dd+"";
if(hh<10)clock+="0";
clock+=hh+":";
if(ii<10)clock+=‘0‘;
clock+=ii+":";
if(ss<10)clock+=‘0‘;
clock+=ss;
document.write(clock);//获取当前日期





3、日期字符串转时间戳



vardate=‘2015-03-0517:59:00.0‘;
date=date.substring(0,19);
date=date.replace(/-/g,‘/‘);
vartimestamp=newDate(date).getTime();
document.write(timestamp);





4、时间戳转日期字符串



vartimestamp=‘1425553097‘;
vard=newDate(timestamp*1000);//根据时间戳生成的时间对象
vardate=(d.getFullYear())+"-"+
(d.getMonth()+1)+"-"+
(d.getDate())+""+
(d.getHours())+":"+
(d.getMinutes())+":"+
(d.getSeconds());
document.write(date);



<script>
functiongetLocalTime(nS){
returnnewDate(parseInt(nS)*1000).toLocaleString().replace(/:\d{1,2}$/,'');
}
alert(getLocalTime(1293072805));
</script>






结果是

2010年12月23日10:53

第二种





<script>
functiongetLocalTime(nS){
returnnewDate(parseInt(nS)*1000).toLocaleString().substr(0,17)}
alert(getLocalTime(1293072805));
</script>





如果你想得到这样格式的怎么办呢?

2010-10-2010:00:00

看下面代码吧



<script>
functiongetLocalTime(nS){
returnnewDate(parseInt(nS)*1000).toLocaleString().replace(/年|月/g,"-").replace(/日/g,"");
}
alert(getLocalTime(1177824835));
</script>



也可以这样写的



functionformatDate(now){
varyear=now.getYear();
varmonth=now.getMonth()+1;
vardate=now.getDate();
varhour=now.getHours();
varminute=now.getMinutes();
varsecond=now.getSeconds();
returnyear+"-"+month+"-"+date+""+hour+":"+minute+":"+second;
}

vard=newDate(1230999938);
alert(formatDate(d));



好了问题解决

需要注意的是

不要把字符串中的Date(这样的字符也传进去,要先处理一下,这样很方便就能处理的

可以使用replace方法

如下:



replace("/Date(","").replace(")/","");




JS时间字符串与时间戳之间的转换


时间:2015-03-0520:46:54阅读:2810评论:0收藏:0[点我收藏+]

标签:


1、当前时间换时间戳



vartimestamp=parseInt(newDate().getTime()/1000);//当前时间戳
document.write(timestamp);





2、当前时间换日期字符串



varnow=newDate();
varyy=now.getFullYear();//年
varmm=now.getMonth()+1;//月
vardd=now.getDate();//日
varhh=now.getHours();//时
varii=now.getMinutes();//分
varss=now.getSeconds();//秒
varclock=yy+"-";
if(mm<10)clock+="0";
clock+=mm+"-";
if(dd<10)clock+="0";
clock+=dd+"";
if(hh<10)clock+="0";
clock+=hh+":";
if(ii<10)clock+=‘0‘;
clock+=ii+":";
if(ss<10)clock+=‘0‘;
clock+=ss;
document.write(clock);//获取当前日期





3、日期字符串转时间戳



vardate=‘2015-03-0517:59:00.0‘;
date=date.substring(0,19);
date=date.replace(/-/g,‘/‘);
vartimestamp=newDate(date).getTime();
document.write(timestamp);





4、时间戳转日期字符串



vartimestamp=‘1425553097‘;
vard=newDate(timestamp*1000);//根据时间戳生成的时间对象
vardate=(d.getFullYear())+"-"+
(d.getMonth()+1)+"-"+
(d.getDate())+""+
(d.getHours())+":"+
(d.getMinutes())+":"+
(d.getSeconds());
document.write(date);



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: