Date类型有一些专门用于将日期格式化为字符串的方法:
1、toDateString()-以特定的于实现的格式显示星期几、年月日。
2、toTimeString()-以特定的于实现的格式显示时分秒和时区。
3、toLocaleDateString()-以特定于地区的格式显示星期几、年月日。
4、toLocalTimeString()-以特定于实现的格式显示时分秒。
5、toUTCString()-以特定于实现的格式完整的UTC日期。
6、toCMTString()-同toUTCString(),目的是确保向后兼容。
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>日期</title> <style> h1 { text-align: center; color:#FFEEFF; } #span { position: relative; left: 50%; margin-left: -70px; text-align: center; color:#FFCD00; } </style></head><body><h1>日期</h1><span id="span"></span><!--钟表显示--><script> // 获取id函数 function my$(id) { return document.getElementById(id); } //钟表函数 getTime(); setInterval(getTime,1000);//定时器启动 function getTime(){ var day=new Date(); var year=day.getFullYear();//年 var month=day.getMonth()+1;//月 var date=day.getDate();//日 var hour=day.getHours();//小时 var minute=day.getMinutes();//分钟 var second=day.getSeconds();//秒 month=month<10?"0"+month:month;//为小于10的时间前面加“0”使格式对齐。 date=date<10?"0"+date:date; hour=hour<10?"0"+hour:hour; minute=minute<10?"0"+minute:minute; second=second<10?"0"+second:second; my$("span").innerText=year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second; }</script></body></html>转载于:https://www.cnblogs.com/hughman/p/6568037.html
相关资源:JAVA上百实例源码以及开源项目