扫码关注官方订阅号
如图,怎么用简单的js脚本的到这样一个年月日和汉字的星期几的日期呢?
认证高级PHP讲师
用时间格式化方法把年月日星期分别取出来然后拼到一起。
项目中用的
Date.prototype.Format = function (fmt) { function setMonth(month) { var engMonth; switch (month) { case 0: engMonth = 'Jan'; break; case 1: engMonth = 'Feb'; break; case 2: engMonth = 'Mar'; break; case 3: engMonth = 'Apr'; break; case 4: engMonth = 'May'; break; case 5: engMonth = 'Jun'; break; case 6: engMonth = 'Jul'; break; case 7: engMonth = 'Aug'; break; case 8: engMonth = 'Sep'; break; case 9: engMonth = 'Oct'; break; case 10: engMonth = 'Nov'; break; case 11: engMonth = 'Dec'; break; } return engMonth } function setTimezone(x) { var timezone,sign; var a = x/-60; sign = a>0 ? '+' : '-'; if (Math.abs(a) < 10) { a = '0' + a; } timezone = sign + a + ':00'; return timezone; } function addZero(a) { var target; target = a < 10 ? '0'+ a : a; return target; } var o = { "M+": setMonth(this.getMonth()), //month "d+": addZero(this.getDate()), //day "h+": this.getHours(), //hour "m+": addZero(this.getMinutes()), //minute "s+": addZero(this.getSeconds()), //seconds "q+": Math.floor((this.getMonth() + 3) / 3), //quarter "S": this.getMilliseconds(), //milliseconds "T": setTimezone(this.getTimezoneOffset()) }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); 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))); return fmt; };
调用
new Date().Format('d-M-yyyy h:m:s T') "15-Jun-2016 17:46:26 +08:00"
没有一个直接的方法,都是获取一个时间戳然后得到年月日之后进行字符串拼接
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
用时间格式化方法把年月日星期分别取出来然后拼到一起。
项目中用的
调用
没有一个直接的方法,都是获取一个时间戳然后得到年月日之后进行字符串拼接