javascript 时间格式方法
2009-08-29 11:00
204 查看
//时间格式设置
Date.prototype.format = function(format) {
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
};
if(/(y+)/.test(format)) {
format = format.replace(RegExp.$1,
(this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(format))
format = format.replace(RegExp.$1,
RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
}
return format;
}
//用法
var d = new Date();
d.format("h");
//判断时间
function convertdate(strdate) {
strdate = strdate.replace(/-/ig,'/');
var d = new Date(strdate);
var now = new Date();
var result;
if (d.getYear() == now.getYear() && d.getMonth() == now.getMonth()) {
var xday = now.getDate() - d.getDate();
switch (xday) {
case 0:
result = "今天 " + d.format("hh") + ":" + d.format("mm") + ":" + d.format("ss");
break;
case 1:
result = "昨天 " + d.format("hh") + ":" + d.format("mm") + ":" + d.format("ss");
break;
case 2:
result = "前天 " + d.format("hh") + ":" + d.format("mm") + ":" + d.format("ss");
break;
default:
result = d.format("yyyy-MM-dd hh:mm");
break;
}
} else {
result = d.format("yyyy-MM-dd hh:mm");
}
return result;
}
相关文章推荐
- javascript中处理时间戳为日期格式的方法
- javascript方法时间差值转为00:00:00格式
- Javascript控制input输入时间格式的方法
- Javascript控制input输入时间格式的方法
- javascript中处理时间戳为日期格式的方法
- javascript中处理时间戳为日期格式的方法
- Windows 8系统更改日期、时间和数字格式的方法
- 准确得到时间格式的方法,C#里面很多的种方法试过后都有问题,只能按此方法没有问题
- JavaScript 时间 格式 转换 及 Date对象 总结
- 在JavaScript中操作时间之getMonth()方法的使用
- MATLAB获取系统时间的方法和格式输出
- JavaScript 日期时间控件,兼容所有浏览器,界面友好,支持多格式,支持多种触发,改进版
- javascript json格式解析方法
- [转]使用Javascript动态创建表格,不同的方法,巨大的运行时间差异!
- JavaScript将机器时间格式转换3秒前,5分钟前
- PHP计算剩余时间的2种方法,返回距离现在时间的格式,如1秒前,1分前..
- 利用javascript/jquery对上传文件格式过滤的方法
- 后台定义方法,前台JS调用设置时间格式
- js方法,将时间戳转换为yyyymmddhhmmss格式
- javaScript输出指定的时间格式