// 获取当前时间戳(以s为单位)
var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000;
//当前时间戳为:1403149534
console.log("当前时间戳为:" + timestamp);
// 获取某个时间格式的时间戳
var stringTime = "2014-07-10 10:21:12";
var timestamp2 = Date.parse(new Date(stringTime));
timestamp2 = timestamp2 / 1000;
//2014-07-10 10:21:12的时间戳为:1404958872
console.log(stringTime + "的时间戳为:" + timestamp2);
// 将当前时间换成时间格式字符串
var timestamp3 = 1403058804;
var newDate = new Date();
newDate.setTime(timestamp3 * 1000);
// Wed Jun 18 2014
console.log(newDate.toDateString());
// Wed, 18 Jun 2014 02:33:24 GMT
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());
// Wed Jun 18 2014 10:33:24 GMT+0800 (中国标准时间)
console.log(newDate.toString());
// 10:33:24 GMT+0800 (中国标准时间)
console.log(newDate.toTimeString());
// Wed, 18 Jun 2014 02:33:24 GMT
console.log(newDate.toUTCString());
Date.prototype.format = function(format) {
var date = {
"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 (var k in date) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1
? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
}
console.log(newDate.format('yyyy-MM-dd h:m:s'));
</script>
1、当前时间换时间戳
var timestamp = parseInt(new Date().getTime()/1000); // 当前时间戳
document.write(timestamp);
2、当前时间换日期字符串
var now = new Date();
var yy = now.getFullYear(); //年
var mm = now.getMonth() + 1; //月
var dd = now.getDate(); //日
var hh = now.getHours(); //时
var ii = now.getMinutes(); //分
var ss = now.getSeconds(); //秒
var clock = 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、日期字符串转时间戳
var date = ‘2015-03-05 17:59:00.0‘;
date = date.substring(0,19);
date = date.replace(/-/g,‘/‘);
var timestamp = new Date(date).getTime();
document.write(timestamp);
4、时间戳转日期字符串
var timestamp = ‘1425553097‘;
var d = new Date(timestamp * 1000); //根据时间戳生成的时间对象
var date = (d.getFullYear()) + "-" +
(d.getMonth() + 1) + "-" +
(d.getDate()) + " " +
(d.getHours()) + ":" +
(d.getMinutes()) + ":" +
(d.getSeconds());
document.write(date);
<script>
function getLocalTime(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:d{1,2}$/,' ');
}
alert(getLocalTime(1293072805));
</script>
结果是
2010年12月23日 10:53
第二种
<script>
function getLocalTime(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17)}
alert(getLocalTime(1293072805));
</script>
如果你想得到这样格式的怎么办呢?
2010-10-20 10:00:00
看下面代码吧
<script>
function getLocalTime(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
}
alert(getLocalTime(1177824835));
</script>
也可以这样写的
function formatDate(now) {
var year=now.getYear();
var month=now.getMonth()+1;
var date=now.getDate();
var hour=now.getHours();
var minute=now.getMinutes();
var second=now.getSeconds();
return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
}
var d=new Date(1230999938);
alert(formatDate(d));
好了问题解决
需要注意的是
不要把字符串中的Date(这样的字符也传进去,要先处理一下,这样很方便 就能处理的
可以使用replace方法
如下:
replace("/Date(","").replace(")/","");
JS 时间字符串与时间戳之间的转换
标签:
1、当前时间换时间戳
var timestamp = parseInt(new Date().getTime()/1000); // 当前时间戳
document.write(timestamp);
2、当前时间换日期字符串
var now = new Date();
var yy = now.getFullYear(); //年
var mm = now.getMonth() + 1; //月
var dd = now.getDate(); //日
var hh = now.getHours(); //时
var ii = now.getMinutes(); //分
var ss = now.getSeconds(); //秒
var clock = 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、日期字符串转时间戳
var date = ‘2015-03-05 17:59:00.0‘;
date = date.substring(0,19);
date = date.replace(/-/g,‘/‘);
var timestamp = new Date(date).getTime();
document.write(timestamp);
4、时间戳转日期字符串
var timestamp = ‘1425553097‘;
var d = new Date(timestamp * 1000); //根据时间戳生成的时间对象
var date = (d.getFullYear()) + "-" +
(d.getMonth() + 1) + "-" +
(d.getDate()) + " " +
(d.getHours()) + ":" +
(d.getMinutes()) + ":" +
(d.getSeconds());
document.write(date);