版權聲明:轉載請注明作者及出處,否則将追究法律責任。 https://blog.csdn.net/q2158798/article/details/79310455
1.JS格式化時間方法
格式一: 2018-1-29-10:34:49
var curr_time = new Date();
Myformatter(curr_time);
function myformatter(date){
var strDate = date.getFullYear()+"-";
strDate += date.getMonth()+1+"-";
strDate += date.getDate()+"-";
strDate += date.getHours()+":";
strDate += date.getMinutes()+":";
strDate += date.getSeconds();
alert("strDate:"+strDate);
return strDate ;
}
格式二: 2018-1-29
function myformatter(date){
var strDate = date.getFullYear()+"-";
strDate += date.getMonth()+1+"-";
strDate += date.getDate();
alert("strDate:"+strDate);
return strDate ;
}
格式三:2018-02-05 (月份和日期小于10的時候 在前面自動加零)
if(date.getMonth()<10){
var s = date.getMonth()+1+"-";
strDate += "0"+s;
}else{
strDate += date.getMonth()+1+"-";
}
if(date.getDate()<10){
strDate += "0"+date.getDate();
}else{
strDate += date.getDate();
return strDate ;
2.Js擷取目前日期時間
var myDate = new Date();
myDate.getYear(); //擷取目前年份(2位)
myDate.getFullYear(); //擷取完整的年份(4位,1970-????)
myDate.getMonth(); //擷取目前月份(0-11,0代表1月)
myDate.getDate(); //擷取目前日(1-31)
myDate.getDay(); //擷取目前星期X(0-6,0代表星期天)
myDate.getTime(); //擷取目前時間(從1970.1.1開始的毫秒數)
myDate.getHours(); //擷取目前小時數(0-23)
myDate.getMinutes(); //擷取目前分鐘數(0-59)
myDate.getSeconds(); //擷取目前秒數(0-59)
myDate.getMilliseconds(); //擷取目前毫秒數(0-999)
myDate.toLocaleDateString(); //擷取目前日期
var mytime=myDate.toLocaleTimeString(); //擷取目前時間
myDate.toLocaleString( ); //擷取日期與時間