昨天写项目,要把时间戳转换成日期格式发给后端 我就去网上找 看到的一些都不是我想要的 索性自己就写了一个如图
下面是angular 模式
$scope.getMyDate = function(str){
str = parseInt(str);
if(str!=""||str!=null){
var oDate = new Date(str);
var oYear = oDate.getFullYear();
var oMonth = oDate.getMonth()+1;
oMonth = oMonth>=10? oMonth:'0'+oMonth;
var oDay = oDate.getDate();
oDay = oDay>=10? oDay:'0'+oDay;
var theDate = oYear+"-"+oMonth+"-"+oDay;
}else{
theDate = "";
}
return theDate
};
下面是js 模式
function getMyDate(str){
str = parseInt(str);
if(str!=""||str!=null){
var oDate = new Date(str);
var oYear = oDate.getFullYear();
var oMonth = oDate.getMonth()+1;
oMonth = oMonth>=10? oMonth:'0'+oMonth;
var oDay = oDate.getDate();
oDay = oDay>=10? oDay:'0'+oDay;
var theDate = oYear+"-"+oMonth+"-"+oDay;
}else{
theDate = "";
}
return theDate
};