<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 日期格式化的更多相关文章

  1. SpringBoot返回date日期格式化

    SpringBoot返回date日期格式化,解决返回为TIMESTAMP时间戳格式或8小时时间差 问题描述 在Spring Boot项目中,使用@RestController注解,返回的java对象中 ...

  2. js Date 日期格式化(转)

    var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();    //获取完整的年份(4位,1 ...

  3. js的 new Date()日期格式化显示以及js获取时间戳

    一.日期格式化显示: 对 new Date() 得到日期的进行格式显示扩展,扩展方法如下: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分 ...

  4. 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 ...

  5. SpringBoot返回date日期格式化,解决返回为TIMESTAMP时间戳格式或8小时时间差

    问题描述 在Spring Boot项目中,使用@RestController注解,返回的java对象中若含有date类型的属性,则默认输出为TIMESTAMP时间戳格式 ,如下所示: 解决方案    ...

  6. date日期 格式化

    这个是别人写的,我拿过来用的,哈哈 Date.prototype.format = function(fmt) { var o = { "M+" : this.getMonth() ...

  7. JS :Date日期格式化

    Date.prototype.format = function (formatStr) { var date = this; /* 函数:填充0字符 参数:value-需要填充的字符串, lengt ...

  8. JQ 日期格式化

    将字符转换为日期格式: function getDate(strDate) { var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$ ...

  9. Java日期格式化方法

    首先获取当前系统时间的方法有两种:第一种可以用currentTimeMillis()方法获取,它其实产生的是一个当前的毫秒数,这个毫秒是自1970年1月1日0时起至现在的毫秒数,类型是long 型,可 ...

随机推荐

  1. django Multi-table inheritance ---- 用于实现基表-子表

    SQL中的父子表.在django中可以直接通过模式的继承来完成! 一.django中的model定义如下: 1.django定义 from django.db import models # Crea ...

  2. Spring注解@ResponseBody,@RequestBody

    @RequestBody 将HTTP请求正文转换为适合的HttpMessageConverter对象. @ResponseBody 将内容或对象作为 HTTP 响应正文返回,并调用适合HttpMess ...

  3. Python强制抛出自定义异常

    raise Exception("My Exception") 当程序运行到这行时,会抛出异常,打印出Exception: My Exception

  4. UIViewController的生命周期及iOS程序执行顺序 和ios6 处理内存警告

    当一个视图控制器被创建,并在屏幕上显示的时候. 代码的执行顺序1. alloc                                   创建对象,分配空间2.init (initWithN ...

  5. iOS开发之使用AFN上传图片

    //1.创建管理者对象 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.responseSerializ ...

  6. C语言 · 利息计算

    算法提高 利息计算   时间限制:1.0s   内存限制:512.0MB      编制程序完成下述任务:接受两个数,一个为用户一年期定期存款金额,一个为按照百分比格式表示的利率:程序计算一年期满 后 ...

  7. [转]PHP 5.3.0以上推荐使用mysqlnd驱动

    我们一般的使用场景,比较少关注PHP版本的问题,而且市面上提供的PHP运行环境都还是5.2系列的. 原文:http://zhangxugg-163-com.iteye.com/blog/1894990 ...

  8. Istio微服务架构初试

    感谢 http://blog.csdn.net/qq_34463875/article/details/77866072 看了一些文档,有些半懂不懂,所以还是需要helloworld一下.因为isti ...

  9. 学会读JQuery等JS插件源码

    看了 http://my249645546.iteye.com/blog/1716629 上的这篇文章感觉挺好的,所以决定转过来,谢谢这位博主. 很多人觉得jquery.ext等一些开源js源代码 十 ...

  10. 数据库 数据库SQL语句四

    多表查询 等值连接 --查询员工信息,员工号,姓名,月薪,部门名称 select e.empno,e.ename,d.dname from emp e,dept d where e.deptno=d. ...