JavaScript如何实现时间日期格式转换
发表于:2025-11-11 作者:千家信息网编辑
千家信息网最后更新 2025年11月11日,小编给大家分享一下JavaScript如何实现时间日期格式转换,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!时间日期格式转换Date.prototype.format = funct
千家信息网最后更新 2025年11月11日JavaScript如何实现时间日期格式转换
小编给大家分享一下JavaScript如何实现时间日期格式转换,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
时间日期格式转换
Date.prototype.format = function(formatStr) {
var str = formatStr;
var Week = ["日", "一", "二", "三", "四", "五", "六"];
str = str.replace(/yyyy|YYYY/, this.getFullYear());
str = str.replace(
/yy|YY/,
this.getYear() % 100 > 9
? (this.getYear() % 100).toString()
: "0" + (this.getYear() % 100)
);
str = str.replace(
/MM/,
this.getMonth() + 1 > 9
? (this.getMonth() + 1).toString()
: "0" + (this.getMonth() + 1)
);
str = str.replace(/M/g, this.getMonth() + 1);
str = str.replace(/w|W/g, Week[this.getDay()]);
str = str.replace(
/dd|DD/,
this.getDate() > 9 ? this.getDate().toString() : "0" + this.getDate()
);
str = str.replace(/d|D/g, this.getDate());
str = str.replace(
/hh|HH/,
this.getHours() > 9 ? this.getHours().toString() : "0" + this.getHours()
);
str = str.replace(/h|H/g, this.getHours());
str = str.replace(
/mm/,
this.getMinutes() > 9
? this.getMinutes().toString()
: "0" + this.getMinutes()
);
str = str.replace(/m/g, this.getMinutes());
str = str.replace(
/ss|SS/,
this.getSeconds() > 9
? this.getSeconds().toString()
: "0" + this.getSeconds()
);
str = str.replace(/s|S/g, this.getSeconds());
return str;
};
// 或
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;
};
alert(new Date().format("yyyy-MM-dd hh:mm:ss"));看完了这篇文章,相信你对"JavaScript如何实现时间日期格式转换"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!
日期
时间
格式
篇文章
完了
更多
知识
行业
资讯
资讯频道
频道
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
kdc服务器
怎么进到腾讯的服务器
怎么打服务器
天津服务器提供商
芙蓉区软件开发高职
你可以修好apex的服务器吗
国内服务器是怎么运行的
酷喵同步服务器失败
网络安全法 网络责任人
厦门市软件开发公司电话
内存 数据库 开源
mysql备份主数据库
数据库字段长度小于10
网络安全威胁企业内部
服务器管理器名称
软件开发项目形象进度
微信 删除服务器记录
浙江新型软件开发价格表格
生活中涉及数据库技术
网络安全整体防御能力
分布式数据库采用的机制是什么
济宁乐搜网络技术有限公司
太原升腾服务器厂家直销
租用香港云服务器
河北lol服务器云空间
锐捷服务器管理员密码
实时型数据库
高级java软件开发证
曲面显示器做软件开发怎么样
软件开发服务收入做账