Date 日期格式化
<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>
Date 日期格式化的更多相关文章
- SpringBoot返回date日期格式化
SpringBoot返回date日期格式化,解决返回为TIMESTAMP时间戳格式或8小时时间差 问题描述 在Spring Boot项目中,使用@RestController注解,返回的java对象中 ...
- js Date 日期格式化(转)
var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份(4位,1 ...
- js的 new Date()日期格式化显示以及js获取时间戳
一.日期格式化显示: 对 new Date() 得到日期的进行格式显示扩展,扩展方法如下: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分 ...
- javascript Date 日期格式化 formatDate, require.js 模块 支持全局js引入 / amd方式加载
* 引入AMD加载方式: require.js CDN https://cdn.bootcss.com/require.js/2.3.5/require.js * 创建模块文件./js/util/d ...
- SpringBoot返回date日期格式化,解决返回为TIMESTAMP时间戳格式或8小时时间差
问题描述 在Spring Boot项目中,使用@RestController注解,返回的java对象中若含有date类型的属性,则默认输出为TIMESTAMP时间戳格式 ,如下所示: 解决方案 ...
- date日期 格式化
这个是别人写的,我拿过来用的,哈哈 Date.prototype.format = function(fmt) { var o = { "M+" : this.getMonth() ...
- JS :Date日期格式化
Date.prototype.format = function (formatStr) { var date = this; /* 函数:填充0字符 参数:value-需要填充的字符串, lengt ...
- JQ 日期格式化
将字符转换为日期格式: function getDate(strDate) { var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$ ...
- Java日期格式化方法
首先获取当前系统时间的方法有两种:第一种可以用currentTimeMillis()方法获取,它其实产生的是一个当前的毫秒数,这个毫秒是自1970年1月1日0时起至现在的毫秒数,类型是long 型,可 ...
随机推荐
- 老生常谈combobox和combotree模糊查询
FIRST /** * combobox和combotree模糊查询 * combotree 结果显示两级父节点(手动设置数量) * 键盘上下键选择叶子节点 * 键盘回车键设置文本的值 */ (fun ...
- POJ 1321:棋盘问题
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21666 Accepted: 10765 Descriptio ...
- Experience on Namenode backup and restore --- checkpoint
Hadoop version: Hadoop 2.2.0.2.0.6.0-0009 Well, We can do this by building Secondary Namenode, Check ...
- action(三)
CCSize boxSize = CCSizeMake(100.0f, 100.0f); CCLayerColor *box = CCLayerColor::create(ccc4(, , , )); ...
- quick cocos2d-x 2.2.4 window环境调试
BabeLua简介 BabeLua是一款基于VS2012/2013(简称VS)的免费开源的Lua集成开发环境,在Lua编辑和调试方面,具有如下功能和特性: ●Lua语法高亮 ●语法检查 ●自动补全 ● ...
- 【转】logstash配置java环境
1.bin/logstash,新增 JAVA_CMD=/home/admin/soft/jdk1.8.0_121/bin JAVA_HOME=/home/admin/soft/jdk1.8.0_121 ...
- 删除CNNIC根证书
操作方法: 1.点击IE工具菜单-->选项-->内容-->证书,在受信任的根证书颁发机构中找到CNNIC Root,将证书导出到桌面备用. 双击CNNIC ROOT查看这个证书的属性 ...
- FreeRTOS 消息队列
以下基础内容转载自安富莱电子: http://forum.armfly.com/forum.php 本章节为大家讲解 FreeRTOS 的一个重要的通信机制----消息队列,初学者要熟练掌握,因为消息 ...
- BAT-把当前用户以管理员权限运行
相关资料: http://jingyan.baidu.com/article/72ee561a5dc24fe16138df95.html 网友求助:联想Y400,Win8系统 怎样获得管理员身份 要求 ...
- Linux编译安装PHP Mysql Nginx
安装gcc g++等编译器 yum -y install gcc gcc-c++ automake autoconf libtool glibc make 安装一些lnmp依赖的库 yum -y in ...