Date 日期格式化
2016-03-29 11:01
316 查看
<span id="time"></span> <script> //名称:日期加法函数 //参数:part(year、month、day、hour、minute、second、millisecond) //返回:Date对象 Date.prototype.add = function (part, value) { if (!value || isNaN(value)) value = 0; switch (part) { case "year": this.setFullYear(this.getFullYear() + value); break; case "month": this.setMonth(this.getMonth() + value); break; case "day": this.setDate(this.getDate() + value); break; case "hour": this.setHours(this.getHours() + value); break; case "minute": this.setMinutes(this.getMinutes() + value); break; case "second": this.setSeconds(this.getSeconds() + value); break; case "millisecond": this.setMilliseconds(this.getMilliseconds() + value); break; default: } return this; }; Date.prototype.addYears = function (value) { if (!value || isNaN(value)) value = 0; this.setFullYear(this.getFullYear() + value); return this; }; Date.prototype.addMonths = function (value) { if (!value || isNaN(value)) value = 0; this.setMonth(this.getMonth() + value); return this; }; Date.prototype.addDays = function (value) { if (!value || isNaN(value)) value = 0; this.setDate(this.getDate() + value); return this; }; Date.prototype.addHours = function (value) { if (!value || isNaN(value)) value = 0; this.setHours(this.getHours() + value); return this; }; Date.prototype.addMinutes = function (value) { if (!value || isNaN(value)) value = 0; this.setMinutes(this.getMinutes() + value); return this; }; Date.prototype.addSeconds = function (value) { if (!value || isNaN(value)) value = 0; this.setSeconds(this.getSeconds() + value); return this; }; Date.prototype.addMilliseconds = function (value) { if (!value || isNaN(value)) value = 0; this.setMilliseconds(this.getMilliseconds() + value); return this; }; //名称:日期加法函数 //参数:time(日期字符串,示例:12:00:00) //返回:Date对象 Date.prototype.addTime = function (time) { var timeRegex = /^([0-1]?\d|2[0-3])(:[0-5]?\d){1,2}$/g; if (timeRegex.test(time)) { var value = Date.parse("1970/1/1 " + time) - Date.parse("1970/1/1"); this.setMilliseconds(this.getMilliseconds() + value); } return this; }; //名称:日期格式化函数 //参数:format(示例:yyyy-MM-dd hh:mm:ss)、zeroize(是否补零) //返回:日期字符串 Date.prototype.toCustomString = function (format, zeroize) { if (!zeroize) zeroize = false; var dy = this.getFullYear(); var dM = this.getMonth() + 1; var dd = this.getDate(); var dh = this.getHours(); var dm = this.getMinutes(); var ds = this.getSeconds(); var dS = this.getMilliseconds(); var orm = { "y+": dy.toString(), "M+": !zeroize ? dM.toString() : dM < 10 ? '0' + dM : dM.toString(), "d+": !zeroize ? dd.toString() : dd < 10 ? '0' + dd : dd.toString(), "h+": !zeroize ? dh.toString() : dh < 10 ? '0' + dh : dh.toString(), "m+": !zeroize ? dm.toString() : dm < 10 ? '0' + dm : dm.toString(), "s+": !zeroize ? ds.toString() : ds < 10 ? '0' + ds : ds.toString(), "S": dS.toString() }; for (var i in orm) { var patt = new RegExp(i); if (patt.test(format)) { var item = orm[i]; var ms = format.match(patt); var result = ms[0]; if (i === "S") { format = format.replace(result, item); } else { format = format.replace(result, item.substr(item.length - result.length)); } } } return format; }; window.onload = function(){ var time = document.getElementById("time"); setInterval('time.innerText = new Date().toCustomString("yyyy-MM-dd hh:mm:ss")',1000); } </script>
没有格式啥的要求的话,就用Date下的toLocaleString()显示年月日时间或者toLocaleDateString()显示年月日
<span id="time"></span> <script> var time = document.getElementById("time"); setInterval('time.innerHTML = new Date().toLocaleString()', 1000); </script>
相关文章推荐
- Hibernate 参数设置一览表
- Java泛型中E、T、K、V等的含义
- CodeIgniter基于Email类发邮件的方法
- 自己实现一个PullToZoomListView放大回弹效果,PullToZoomView源码解析
- Java 回顾笔记_集合框架-泛型基本应用
- Scala基本语法和概念
- 【BZOJ4196】[Noi2015]软件包管理器【树链剖分】【线段树】
- 翻翻git之---简单易用的状态栏工具库 StatusBarUtil
- 将博客搬至CSDN
- 翻翻git之---简单易用的状态栏工具库 StatusBarUtil
- GPT WIN 换硬盘 硬盘克隆或复制 无法确定的问题,硬盘大小不一致换系统。
- iOS中OpenURL的使用
- 隐藏状态栏时,整体布局不会往上挪的办法
- iOS 进阶之路 2
- delphi 解决屏幕闪烁
- Linux程序设计-套接字编程-网络信息服务
- php 计算程序的执行时间
- java变量
- string与wstring转换
- No_16_0328 Virtex6 PCIe2.5 仿真学习