js时间处理详解编程语言

function CurentTime() 
{  
var now = new Date(); 
var year = now.getFullYear();       // 
var month = now.getMonth() + 1;     // 
var day = now.getDate();            // 
var hh = now.getHours();            // 
var mm = now.getMinutes();          // 
var clock = year + "/"; 
if(month < 10) 
clock += "0"; 
clock += month + "/"; 
if(day < 10) 
clock += "0"; 
clock += day + " "; 
if(hh < 10) 
clock += "0"; 
clock += hh + ":"; 
if (mm < 10) clock += '0';  
clock += mm;  
return(clock);  
}  
function CurentTime2() 
{  
var now = new Date(); 
var year = now.getFullYear();       // 
var month = now.getMonth() + 1;     // 
var day = now.getDate();            // 
var hh = now.getHours();            // 
var mm = now.getMinutes();          // 
var ss = now.getSeconds(); 
var clock = year + "/"; 
if(month < 10) 
clock += "0"; 
clock += month + "/"; 
if(day < 10) 
clock += "0"; 
clock += day + " "; 
if(hh < 10) 
clock += "0"; 
clock += hh + ":"; 
if (mm < 10) clock += '0';  
clock += mm + ":";  
if (ss < 10) clock += '0'; 
clock += ss; 
return(clock);  
}  
function formate_time(str){ 
var time = new Date(str); 
var year = time.getFullYear();       // 
var month = time.getMonth() + 1;     // 
var day = time.getDate();            // 
//var hh = time.getHours();            //时 
//var mm = time.getMinutes();          //分 
var clock = year + "/"; 
if(month < 10) 
clock += "0"; 
clock += month + "/"; 
if(day < 10) 
clock += "0"; 
clock += day; 
return(clock);  
} 
function formate_time2(str){ 
var time = new Date(str); 
var year = time.getFullYear();       // 
var month = time.getMonth() + 1;     // 
var day = time.getDate();            // 
var hh = time.getHours();            // 
var mm = time.getMinutes();          // 
var ss = time.getSeconds(); 
var clock = year + "/"; 
if(month < 10) 
clock += "0"; 
clock += month + "/"; 
if(day < 10) 
clock += "0"; 
clock += day + " "; 
if(hh < 10) 
clock += "0"; 
clock += hh + ":"; 
if (mm < 10) clock += '0'; 
clock += mm + ":";  
if (ss < 10) clock += '0'; 
clock += ss; 
return(clock);  
} 
function formate_time3(str){ 
var time = new Date(str); 
var year = time.getFullYear();       // 
var month = time.getMonth() + 1;     // 
var day = time.getDate();            // 
var hh = time.getHours();            // 
var mm = time.getMinutes();          // 
// var ss = time.getSeconds(); 
var clock = year + "/"; 
if(month < 10) 
clock += "0"; 
clock += month + "/"; 
if(day < 10) 
clock += "0"; 
clock += day + " "; 
if(hh < 10) 
clock += "0"; 
clock += hh + ":"; 
if (mm < 10) clock += '0';  
clock += mm; 
//clock += mm + ":";  
//if (ss < 10) clock += '0'; 
//clock += ss; 
return(clock);  
}

 

Date.prototype.format = function(format) 
{ 
var o = { 
"M+" : this.getMonth()+1, //month 
"d+" : this.getDate(),    //day 
"h+" : this.getHours(),   //hour 
"m+" : this.getMinutes(), //minute 
"s+" : this.getSeconds(), //second 
"q+" : Math.floor((this.getMonth()+3)/3),  //quarter 
"S" : this.getMilliseconds() //millisecond 
 } 
if(/(y+)/.test(format)) format=format.replace(RegExp.$1, 
(this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
for(var k in o)if(new RegExp("("+ k +")").test(format)) 
format = format.replace(RegExp.$1, 
RegExp.$1.length==1 ? o[k] : 
("00"+ o[k]).substr((""+ o[k]).length)); 
return format; 
} 
日期格式化 
var d = new Date(); 
d.format('yyyy-MM-dd'); 

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/13294.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论