js日期格式化函数实例
发布时间:2015-07-01 编辑:jiaochengji.com
本文介绍了js日期格式化函数的用法,有关js日期格式化的例子,如何用js实现日期与时间的格式化,有需要的朋友参考下吧。
例子,js日期格式化函数实现代码。
复制代码 代码示例:
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 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;
}
例子,js日期格式化代码。
复制代码 代码示例:
<script type="text/javascript">
date.prototype.pattern=function(fmt) {
var o = {
"m+" : this.getmonth()+1, //月份
"d+" : this.getdate(), //日
"h+" : this.gethours()%12 == 0 ? 12 : this.gethours()%12, //小时
"h+" : this.gethours(), //小时
"m+" : this.getminutes(), //分
"s+" : this.getseconds(), //秒
"q+" : math.floor((this.getmonth()+3)/3), //季度
"s" : this.getmilliseconds() //毫秒
};
var week = {
"0" : "\u65e5",
"1" : "\u4e00",
"2" : "\u4e8c",
"3" : "\u4e09",
"4" : "\u56db",
"5" : "\u4e94",
"6" : "\u516d"
};
if(/(y+)/.test(fmt)){
fmt=fmt.replace(regexp.$1, (this.getfullyear()+"").substr(4 - regexp.$1.length));
}
if(/(e+)/.test(fmt)){
fmt=fmt.replace(regexp.$1, ((regexp.$1.length>1) ? (regexp.$1.length>2 ? "\u661f\u671f" : "\u5468") : "")+week[this.getday()+""]);
}
for(var k in o){
if(new regexp("("+ k +")").test(fmt)){
fmt = fmt.replace(regexp.$1, (regexp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
}
} (教程集 www.jiaochengji.com)
return fmt;
}
function returndetail(data)
{
if(data!=null)
{
dwrutil.setvalue("time1",data[0].pattern("yyyy-mm-dd hh:mm:ss"));
dwrutil.setvalue("question",data[1]);
dwrutil.setvalue("result",data[2]);
dwrutil.setvalue("resovor",data[3]);
}
}
</script>
date.prototype.pattern=function(fmt) {
var o = {
"m+" : this.getmonth()+1, //月份
"d+" : this.getdate(), //日
"h+" : this.gethours()%12 == 0 ? 12 : this.gethours()%12, //小时
"h+" : this.gethours(), //小时
"m+" : this.getminutes(), //分
"s+" : this.getseconds(), //秒
"q+" : math.floor((this.getmonth()+3)/3), //季度
"s" : this.getmilliseconds() //毫秒
};
var week = {
"0" : "\u65e5",
"1" : "\u4e00",
"2" : "\u4e8c",
"3" : "\u4e09",
"4" : "\u56db",
"5" : "\u4e94",
"6" : "\u516d"
};
if(/(y+)/.test(fmt)){
fmt=fmt.replace(regexp.$1, (this.getfullyear()+"").substr(4 - regexp.$1.length));
}
if(/(e+)/.test(fmt)){
fmt=fmt.replace(regexp.$1, ((regexp.$1.length>1) ? (regexp.$1.length>2 ? "\u661f\u671f" : "\u5468") : "")+week[this.getday()+""]);
}
for(var k in o){
if(new regexp("("+ k +")").test(fmt)){
fmt = fmt.replace(regexp.$1, (regexp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
}
} (教程集 www.jiaochengji.com)
return fmt;
}
function returndetail(data)
{
if(data!=null)
{
dwrutil.setvalue("time1",data[0].pattern("yyyy-mm-dd hh:mm:ss"));
dwrutil.setvalue("question",data[1]);
dwrutil.setvalue("result",data[2]);
dwrutil.setvalue("resovor",data[3]);
}
}
</script>
您可能感兴趣的文章:
javascript日期计算与格式化日期
javascript 格式化时间日期函数代码
javascript日期对象格式化为字符串
js按指定格式显示日期时间的代码
javascript日期格式化简单例子
js格式化日期时间数据函数代码
js 日期格式化的例子
js字符串日期格式化为yyyy-mm-dd
js long日期格式转为标准日期格式的代码
php日期函数的简单示例代码
上一篇:js将光标聚焦在文本最后怎么实现
下一篇:js 格式化日期二种方法
[关闭]