当前位置:首页 >> 网络编程

Js获取当前日期时间及格式化代码

本文为大家分享了Js获取当前日期时间及格式化操作,具体内容如下

var myDate = new Date();
myDate.getYear();    //获取当前年份(2位)
myDate.getFullYear();  //获取完整的年份(4位,1970-"htmlcode">
//--------------------------------------------------- 
// 判断闰年 
//--------------------------------------------------- 
Date.prototype.isLeapYear = function()  
{  
  return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0)));  
}  
 
//--------------------------------------------------- 
// 日期格式化 
// 格式 YYYY/yyyy/YY/yy 表示年份 
// MM/M 月份 
// W/w 星期 
// dd/DD/d/D 日期 
// hh/HH/h/H 时间 
// mm/m 分钟 
// ss/SS/s/S 秒 
//--------------------------------------------------- 
Date.prototype.Format = function(formatStr)  
{  
  var str = formatStr;  
  var Week = ['日','一','二','三','四','五','六']; 
 
  str=str.replace(/yyyy|YYYY/,this.getFullYear());  
  str=str.replace(/yy|YY/,(this.getYear() % 100)>9"htmlcode">
 //本地时钟
function clockon() {
  var now = new Date();
  var year = now.getFullYear(); //getFullYear getYear
  var month = now.getMonth();
  var date = now.getDate();
  var day = now.getDay();
  var hour = now.getHours();
  var minu = now.getMinutes();
  var sec = now.getSeconds();
  var week;
  month = month + 1;
  if (month < 10) month = "0" + month;
  if (date < 10) date = "0" + date;
  if (hour < 10) hour = "0" + hour;
  if (minu < 10) minu = "0" + minu;
  if (sec < 10) sec = "0" + sec;
  var arr_week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
  week = arr_week[day];
  var time = "";
  time = year + "年" + month + "月" + date + "日" + " " + hour + ":" + minu + ":" + sec + " " + week;
 
  $("#bgclock").html(time);
 
  var timer = setTimeout("clockon()", 200);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。