js方法将系统当前时间转换为字符串详解编程语言

js方法将系统时间转换为字符串案例:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>js将当前时间转换为字符串日期</title> 
<script type="text/javascript" src="jquery-1.11.1.min.js"></script> 
<script type="text/javascript"> 
    $(function () { 
	   $("#clickDate").click(function () { 
		   var date=new Date(); 
		   alert("date:"+date); 
		   dateStr=dateToString(date); 
		   alert("dateStr:"+dateStr); 
	   }); 
 
	}); 
</script> 
<script type="text/javascript"> 
  function dateToString(now){ 
    var year = now.getFullYear(); 
    var month =(now.getMonth() + 1).toString(); 
    var day = (now.getDate()).toString(); 
    var hour = (now.getHours()).toString(); 
    var minute = (now.getMinutes()).toString(); 
    var second = (now.getSeconds()).toString(); 
    if (month.length == 1) { 
        month = "0" + month; 
    } 
    if (day.length == 1) { 
        day = "0" + day; 
    } 
    if (hour.length == 1) { 
    	hour = "0" + hour; 
    } 
    if (minute.length == 1) { 
    	minute = "0" + minute; 
    } 
    if (second.length == 1) { 
    	second = "0" + second; 
    } 
     var dateTime = year + "-" + month + "-" + day +" "+ hour +":"+minute+":"+second; 
     return dateTime; 
  } 
</script> 
</head> 
<body> 
	 <button  id="clickDate">系统当前时间转换为字符串</button> 
</body> 
</html> 

备注:在实际开发中可以获得2个时间进行比较,时间必须已字符串的形式,否则无法进行比较

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

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

相关推荐

发表回复

登录后才能评论