关闭

JS获取当前时间和星期几,并制作时钟效果 jquery

标签: jqueryjshtml
412人阅读 评论(0) 收藏 举报
<pre name="code" class="javascript">//页面加载时执行
$(function(){
<span style="white-space:pre">	</span>//每个1秒 执行showDate方法,显示时间,出现时钟效果
	setInterval(function(){
		showDate();
	},1000);
});


/*
<span style="font-family: Arial, Helvetica, sans-serif;">  * 获取系统当前日期  格式:</span><span style="font-family: Arial, Helvetica, sans-serif;">2014-10-20 21:49:38  星期一</span><span style="font-family: Arial, Helvetica, sans-serif;">        </span>
<div style="padding: 0px 10px; float: left;"></div> <span style="font-family: Arial, Helvetica, sans-serif;">*/</span>
function getCurrentDate(){
	var curr_time = new Date();
	var year      = curr_time.getFullYear();
	var month     = curr_time.getMonth()+1;
	var day       = curr_time.getDate();
	var hour      = curr_time.getHours();
	var minute    = curr_time.getMinutes();
	var second    = curr_time.getSeconds();
	var week      = getWeek();
	var strDate   = year+"-"+(month<10?('0'+month):month)+"-"+(day<10?('0'+day):day)+" "+hour+":"+(minute<10?('0'+minute):minute)+":"+(second<10?('0'+second):second)+"  "+week;
	return strDate;
}


/**
 * 获取当前日期为星期几
 * @param weeknum
 */
function getWeek(){
	var week;
	var curr_time = new Date();
	var weeknum = curr_time.getDay();
	
	if(weeknum == 0) week = "星期日";
	if(weeknum == 1) week = "星期一";
	if(weeknum == 2) week = "星期二";
	if(weeknum == 3) week = "星期三";
	if(weeknum == 4) week = "星期四";
	if(weeknum == 5) week = "星期五";
	if(weeknum == 6) week = "星期六";
	return week;
}

/**
 * 日期显示
 * 替换掉 div中的span的内容
 */
function showDate(){
	var strDate = getCurrentDate();
	$("#dateList").replaceWith("<span id='dateList'>  " +strDate+"</span>");
}

//HTML页面中,显示时间,替换掉<span>中的内容
<div style="padding-bottom: 13px;float: left;"><span id="dateList"></span></div>



   
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:21000次
    • 积分:402
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:19篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论