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 型,可 ...
随机推荐
- Mysql 的位运算符详解,mysql的优先级
位运算是将给定的操作数转化为二进制后,对各个操作数每一位都进行指定的逻辑运算,得到的二进制结果转换为十进制数后就是位运算的结果.MySQL 5.0 支持6 种位运算符,如表4-4 所示. 可以发现,位 ...
- unity, 忽略碰撞
一,layer之间忽略碰撞. Edit->Project Settings->Physics->Layer Collision Matrix 二,collider之间忽略碰撞. vo ...
- Facebook Oauth2.0 API调用方法
这些天搞了下Facebook API的东东,在官方网站下弄了一些接口,下面简单的把facebook的调用流程以及常用接口书序一下 :-) 当然在使用facebook api之前要有facebook账 ...
- ubuntu下禁用和恢复触摸板
1.一般禁用选项在 settings > mouse and touchpad 中.(16.04通过实验)如果无法禁用或者希望恢复,向下看. 2.命令行键入: xinput ,插卡touchpa ...
- 分享几个linux系统版本的查看命令
发布:theboy 来源:net [大 中 小] 查看linux系统版本的命令 有如下命令可供参考: # lsb_release -a LSB Version: :core-3.1-ia ...
- Error-Project facet Java version 1.8 is not supported
最近导入最新的Strtus2.5.10.1 Demo时出现了这个错误 解决方案如下: 选中工程——右键——Properties 然后依次展开找到如图所示内容,将1.8改成1.7即可. 原因:工程默认配 ...
- ZOJ 2610 Puzzle 模拟
大模拟:枚举6个方向.检查每一个0是否能移动 Puzzle Time Limit: 2 Seconds Memory Limit: 65536 KB Little Georgie likes ...
- bazel-编译多目标
demo2 使用bazel编译多目标示例,一个bianry,一个library. demo2目录树 ── demo2 ├── app │ ├── BUILD │ ├── func.cpp ...
- widnows 使用WIN32 APi 实现修改另一打开程序的窗口显示方式
1.GUI点击打开一个程序那边做一个判断. hwnd = 获取目标程序窗口句柄: if(hwnd == NULL /*不存在目标程序窗口句柄*/){ 创建进程,打开目标程序: } else{ ...
- Ununtu 15.04 安装MySql(Django连接Mysql)
本文介绍Ubuntu 15.04下安装MySQL ubuntu 15.04安装mysql django项目连接mysql 一.安装数据库 1.sudo apt-get install mysql-se ...