加入收藏 收藏网站 设为首页 设为首页
招生考试网
学历类| 阳光高考 研 究 生 自学考试 成人高考 专 升 本 中考会考 外语类| 四 六 级 职称英语 商务英语 公共英语 日语能力
资格类| 公 务 员 报 关 员 银行从业 司法考试 导 游 证 教师资格 财会类| 会 计 证 经 济 师 会计职称 注册会计 税 务 师
工程类| 一级建造 二级建造 造 价 师 造 价 员 咨 询 师 监 理 师 医学类| 卫生资格 执业医师 执业药师 执业护士 国际护士
计算机| 等级考试 软件水平 应用能力 其它类| 书画等级 美国高考 驾 驶 员 书法等级 少儿英语 报 检 员 单 证 员 出国留学
 招生考试网 - 计算机等级考试 - 考试辅导 - 正文

 
辅导:JS日期时间函数
来源:fjzsksw.com 2009-11-26 编辑:yangmeiling 【字体:小 大】

Date.prototype.isLeapYear 判断闰年

  Date.prototype.Format 日期格式化

  Date.prototype.DateAdd 日期计算

  Date.prototype.DateDiff 比较日期差

  Date.prototype.toString 日期转字符串

  Date.prototype.toArray 日期分割为数组

  Date.prototype.DatePart 取日期的部分信息

  Date.prototype.MaxDayOfDate 取日期所在月的最大天数

  Date.prototype.WeekNumOfYear 判断日期所在年的第几周

  StringToDate 字符串转日期型

  IsValidDate 验证日期有效性

  CheckDateTime 完整日期时间检查

  daysBetween 日期天数差

  js 代码

  //---------------------------------------------------

  // 判断闰年

  //---------------------------------------------------

  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?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));

  str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + this.getMonth());

  str=str.replace(/M/g,this.getMonth());

  str=str.replace(/w|W/g,Week[this.getDay()]);

  str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());

  str=str.replace(/d|D/g,this.getDate());

  str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());

  str=str.replace(/h|H/g,this.getHours());

  str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());

  str=str.replace(/m/g,this.getMinutes());

  str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());

  str=str.replace(/s|S/g,this.getSeconds());

  return str;

  }

  //+---------------------------------------------------

  //| 求两个时间的天数差 日期格式为 YYYY-MM-dd

  //+---------------------------------------------------

  function daysBetween(DateOne,DateTwo)

  {

  var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'));

  var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1);

  var OneYear = DateOne.substring(0,DateOne.indexOf ('-'));

  var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'));

  var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1);

  var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'));

  var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);

  return Math.abs(cha);

  }



 
  • 上一个文章:

  • 下一个文章:
  • 网站版权与免责声明
    ①由于各方面情况的不断调整与变化,本网所提供的相关信息请以权威部门公布的正式信息为准.
    ②本网转载的文/图等稿件出于非商业性目的,如转载稿涉及版权等问题,请在两周内来电联系.
    最新文章
    热门文章

    报名考试
    全国 | 黑龙江 | 吉林 | 辽宁 | 内蒙古
    青海 | 宁夏 | 甘肃 | 新疆 | 陕西
    西藏 | 北京 | 天津 | 河北 | 山东
    江苏 | 安徽 | 河南 | 上海 | 浙江
    福建 | 广东 | 山西 | 湖南 | 湖北
    江西 | 广西 | 海南 | 云南 | 贵州
    四川 | 重庆
    分省高校计算机考试
    黑龙江 | 吉林 | 辽宁 | 内蒙古 | 河北
    北京 | 天津 | 新疆 | 甘肃 | 宁夏
    青海 | 陕西 | 山西 | 河南 | 山东
    江苏 | 安徽 | 浙江 | 福建 | 广东
    海南 | 广西 | 江西 | 湖北 | 湖南
    四川 | 上海 | 重庆 | 贵州 | 云南
    西藏
    成绩查询
    报考指南
    试题答案
    模拟试题
    考试辅导
    计算机一级 | 计算机二级 | 计算机三级 | 计算机四级
    经验交流
    高校计算机