今天在项目中遇到了将后台数据传过来的json格式的数据转化为标准格式,传到页面的是时间戳格式,所以需要把时间格式化一下,这就需要js来做这件事了。所需知识:1、parseInt()函数:可解析一个字符串,并返回一个整数。解析的全部是 数字。...
<script>
function formatyn(nS) {
var a= parseInt(nS.replace(/\D/igm, ""));
var datetime = new Date(a);
var year = datetime.getFullYear();
var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
return year + "-" + month + "-" + date;
}
</script>
<script>
function formatyn(nS) {
var a = parseInt(nS.replace(/\D/igm, ""));
var datetime = new Date(a);
var year = datetime.getFullYear();
var month = datetime.getMonth() + 1;
var date = datetime.getDate();
return year + "-" + month + "-" + date ;
</script>
replace("/Date(","").replace(")/","");
