一个JS的问题,求大虾的帮助。谢谢~~

MinLin 2013-08-16 02:24:02
JS脚本里,如何将日期变量的格式改为字符,
比如将2006/04/04 14:20:34改为"2006-04-04"?
...全文
77 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sjbcl2008 2013-08-16
  • 打赏
  • 举报
回复
引用 8 楼 kongwei521 的回复:
function test() {
			var str = "2006/04/04 14:20:34";
			var nowStr = new Date(str);
			alert(nowStr.format("yyyy-MM-dd"));
		}
		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;
		};
输出2006-04-04
这样是等于重写了日期的foramt
蝶恋花雨 2013-08-16
  • 打赏
  • 举报
回复
function test() {
			var str = "2006/04/04 14:20:34";
			var nowStr = new Date(str);
			alert(nowStr.format("yyyy-MM-dd"));
		}
		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;
		};
输出2006-04-04
MinLin 2013-08-16
  • 打赏
  • 举报
回复
引用 6 楼 wwqisis 的回复:
[quote=引用 4 楼 ymq_2012 的回复:] Substring(0,10).Replace('/','-')
变量点Substring(0,10).Replace('/','-') ?[/quote] var a = type[i]["SUBMITTIME"] alert(a); a = a.Substring(0, 10).Replace('/', '-'); alert(a);
MinLin 2013-08-16
  • 打赏
  • 举报
回复
引用 4 楼 ymq_2012 的回复:
Substring(0,10).Replace('/','-')
变量点Substring(0,10).Replace('/','-') ?
MinLin 2013-08-16
  • 打赏
  • 举报
回复
引用 2 楼 a346729576 的回复:
http://www.cnblogs.com/atree/archive/2011/06/29/JS-JavaScript-Date-Format.html
还是没找到解决的方法。。。
  • 打赏
  • 举报
回复
Substring(0,10).Replace('/','-')
lucky-lucky 2013-08-16
  • 打赏
  • 举报
回复
使用正则表达式将/替换成-然后取出前面10个字符
MinLin 2013-08-16
  • 打赏
  • 举报
回复
人在线, 求帮助了。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧