js 时间格式与时间戳的相互转换和计算几天后的日期是哪一天

//把日期转换成时间戳

function get_unix_time(time1){
    var newstr = time1.replace(/-/g,'/');
    var date =  new Date(newstr);
    var time_str = date.getTime().toString();
    return time_str.substr(0, 10);
}

一.时间转换时间戳
function transdate(endTime){
var date=new Date();
date.setFullYear(endTime.substring(0,4));
date.setMonth(endTime.substring(5,7)-1);
date.setDate(endTime.substring(8,10));
date.setHours(endTime.substring(11,13));
date.setMinutes(endTime.substring(14,16));
date.setSeconds(endTime.substring(17,19));
return Date.parse(date)/1000;
}
二.时间戳转换时间
(1):转换成 2011-3-16 16:50:43 格式:

function getDate(tm){
var tt=new Date(parseInt(tm) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ")
return tt;
}
(2):转换成 2011年3月16日 16:50:43:

function getDate(tm){
var tt=new Date(parseInt(tm) * 1000).toLocaleString()
return tt;
}

(3):转换成 2011年3月16日 16:50

function getDate(tm){
var tt=new Date(parseInt(tm) * 1000).toLocaleString().substr(0,16);
return tt;
}

//计算几天后的日期,js天数计算、时间计算

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>计算几天后的日期,js天数计算、时间计算-www.jbxue.com</title>
<style type="text/css">
<!--
body {  font-family: "宋体"; font-size: 9pt; margin-top: 0px; margin-left: 0px; margin-right: 0px}
A { COLOR: black; FONT-SIZE: 9pt; FONT-WEIGHT: 400; TEXT-DECORATION: none }
A:hover { COLOR: red; FONT-SIZE: 9pt; FONT-WEIGHT: 400; TEXT-DECORATION: underline }
a:active     { font: 9pt "宋体"; cursor: hand; color: #FF0033 }
-->
</style>
</head>
<body>

<center><font color=red face="隶书" size=6>记录几天后的日期</font></center>
<center>
<table border=0 bordercolor=blue borderlight=green style="border-collapse: collapse" cellpadding="0" cellspacing="0">
<tr><td align=center><font size=5 color=red face="Arial, Helvetica, sans-serif"><strong>下面为效果显示区!</strong></font></td></tr><tr><td align=center> <script language="JavaScript">
<!-- Begin
function AddDays(form) {
  DaysToAdd=document.form.DaysToAdd.value;
  var now=new Date();
  var newdate=new Date();
  var newtimems=newdate.getTime()+(DaysToAdd*24*60*60*1000);
  newdate.setTime(newtimems);
  document.form.display.value=newdate.toLocaleString();
}
// End -->
</script>
<form name=form>
<input type=text name=DaysToAdd size=4 value=10 onFocus="this.select()" onMouseOver="this.focus()">
<input type=button value="天后的日期将是.." onClick="AddDays(this.form)" name="button">
<input type=text name="display" size=20 value="">
</form>
</td></tr>
</table></center>
<center>
<SCRIPT LANGUAGE="JavaScript">
<!-- hide
function goHist(a)
{//www.jbxue.com
   history.go(a);
}
//-->
</script></center>
</body>
</html>

原文地址:https://www.cnblogs.com/dearxinli/p/4086498.html